linux - Merge two Json files with awk in bash -


i try merge 2 json files in 1 file, 1 condition, e.g : if file 1 has example : testa:result>fail , file b contains same line different result e.g : testa:result>pass, file 1 update results file 2, have line awk command merge files without condition :

awk 'begin{print "{"} fnr > 1 && last_file == filename {print line} fnr == 1 {line = ""} fnr==1 && fnr != nr {printf ","} fnr > 1 {line = $0} {last_file = filename} end{print "}"}' json_files/* > json_files/all_merged.json 

please need this.

i suggest use python task. consider example:

import json pprint import pprint  filename1 = "file1.json" # file1 contains { "testname": "color", "result": "fail" } filename2 = "file2.json" # file2 contains { "anothertestname": "color2", "result": "pass" } filenameto = "file3.json"  def visualize(data, prompt):     print(prompt)     pprint(data)     raw_input("press <enter> continue: ") # line pauses execution  def loaddata(fname):     open(fname, "r") f:         return json.load(f)  jd1 = loaddata(filename1) visualize(jd1, "data %s" % filename1) jd2 = loaddata(filename2) visualize(jd2, "data %s" % filename2) jd3 = jd1.copy() # create copy of data file1, step can avoided if don't need unmodified jd1 visualize(jd3, "data after copying") jd3.update(jd2) # merge copy of data file1 file2, updating corresponding keys visualize(jd3, "data after merging") open("file3.json", "w") f3:     json.dump(jd3, f3) 

this version of script gained "interactivity", let control data state on each step of execution. i've checked performs data merging expected. please try run against test data , @ output.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -