How to merge json file using jq?

How to merge json file using jq?

jq merge all json files in directory
merge json files javascript
merge json files online
java merge json files
jq add object to json file
php merge multiple json files
powershell merge multiple json files
jq if

I'm using the jq tools (jq-json-processor) in shell script to parse json.

I've got 2 json files and want to merge them into one unique file

Here the content of files:

file1:

{"tag_id" : ["t1"], "inst_id" : "s1"}
{"tag_id" : ["t1"], "inst_id" : "s2"}

file2:

{"tag_id" : ["t2"], "inst_id" : "s1"}
{"tag_id" : ["t2"], "inst_id" : "s2"}
{"tag_id" : ["t2"], "inst_id" : "s3"}

expected result:

{"tag_id" : ["t1","t2"], "inst_id" : "s1"}
{"tag_id" : ["t1","t2"], "inst_id" : "s2"}
{"tag_id" : ["t2"], "inst_id" : "s3"}

Here's a join-like approach. It assumes your jq has INDEX/2 and supports the --slurpfile command-line option. If your jq does not have these, now would be a good time to upgrade, though there are easy workarounds.

Invocation
jq -n --slurpfile file1 file1.json -f join.jq file2.json
join.jq
def join(s2; joinField; field):
  INDEX(.[]; joinField) 
  | reduce s2 as $x (.;
      ($x|joinField) as $key
      | if .[$key] then (.[$key]|field) += ($x|field)
        else .[$key] = $x
      end )
  | .[]
  ;

$file1 | join(inputs; .inst_id; .tag_id)

How to merge 2 JSON objects from 2 files using jq?, If I understand your question correctly, you just want to output an array which contains the object in each file. In this case, -s will already do that  jq -s '.[0] + .[1]' config.json config-user.json (Note: if you want to merge inner objects instead of just overwriting the left file ones with the right file ones, you will need to do it manually)


One way is to use group_by:

jq -n --slurpfile file1 file1.json --slurpfile file2 file2.json -f merge.jq

where merge.jq contains:

def sigma(f): reduce f as $x (null; . + $x);

$file1 + $file2
| group_by(.inst_id)[]
| {tag_id: sigma(.[].tag_id), inst_id: .[0].inst_id }

How to merge json files using jq or any tool?, thx · Issue #805 · stedolan/jq · GitHub. Skip to content. Sign up. Type a period and the key name without a space between them. This creates a filter from the key name. We also need to tell jq which JSON file to use. We type the following to retrieve the message value: jq .message iss.json. jq prints the text of the message value in the terminal window.


The following approach is very efficient in that:

(a) it takes advantage of the fact that file1.json and file2.json are streams of objects, thus avoiding the memory required to store these objects as arrays;

(b) it avoids sorting (as entailed, for example, by group_by)

The key concept is the keywise-addition of objects. For performing keywise-addition of objects in a stream, we define the following generic function:

# s is assumed to be a stream of mutually
# compatible objects in the sense that, given
# any key of any object, the values at that key
# must be compatible w.r.t. `add`
def keywise_add(s):
  reduce s as $x ({};
     reduce ($x|keys_unsorted)[] as $k (.; 
       .[$k] += $x[$k]));

The task can now be accomplished as follows:

keywise_add(inputs | {(.inst_id): .tag_id} )
| keys_unsorted[] as $k
| {tag_id: .[$k], inst_id: $k}
Invocation

With the above program in add.jq, the invocation:

jq -c -n -f add.jq file1.json file2.json

yields:

{"tag_id":["t1","t2"],"inst_id":"s1"}
{"tag_id":["t1","t2"],"inst_id":"s2"}
{"tag_id":["t2"],"inst_id":"s3"}
Caveat

The above assumes that inst_id is string-valued. If that is not the case, then the above approach can still be used so long as there are no collisions amongst inst_id|tostring, which would be the case, for example, if inst_id were always numeric.

How to merge 2 files json in one with 1 array only for all ? thx · Issue , I have 2 json files with common key and I need to JOIN them As /49037956/​how-to-merge-arrays-from-two-files-into-one-array-with-jq/  I need all the values for keys which exist in both files, copied from source.json to target.json, considering all the levels. I've seen and tested the solution from this post . It only copies the first level of keys, and I couldn't get it to do what I need.


join two json files based on common key · Issue #1247 · stedolan/jq , I try to using the jq tools (jq-json-processor) in shell script to merge the fields two json. Here the content of files: First file { "username": "Bob", "password":  Other assumptions are that you are using jq 1.5 or later, and of course that all the *.json files in the current directory are relevant and that *.json lists them in the desired order. If the ordering of the JSON files is best determined by explicitly listing them, then you will probably want to create a batch file.


Merge with jq command the fields of two json files - json - html, to use the command-line utility jq to filter and parse complex JSON files By the end of the lesson, you will understand how to combine basic  How to use pipes with jq. Because jq is UNIX friendly it is possible to pipe data in and out of it. This can be useful for using jq as a filter or interacting with other tools. In the following pipeline cat pipes the file into jq and this is piped onto less. This can be very useful for viewing large JSON files. cat names.json | jq '.' | less


Reshaping JSON with jq, How to install jq in the most used Linux distributions or compile it from source; How to use jq to parse json-formatted data; How to combine  I want to merge multiple json files into one file in python. The thing that I want to do is if there are several .json files like: # temp1.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'