jq: how to add elements from an array into another array one by one

727 views jq
10

First array file1:

[{"1":"2"}, {"3":"4"}] 

Second array in file2:

[{"a":"b"}, {"c":"d"}]

I tried "jq -s -c '.[0][] + .[1][]' file1 file2". The result:

{"1":"2","a":"b"}
{"3":"4","a":"b"}
{"1":"2","c":"d"}
{"3":"4","c":"d"}

However, I want the following:

{"1":"2","a":"b"}
{"3":"4","c":"d"}

Thanks for the help.

answered question

1 Answer

1

An (the?) elegant way:

$ jq -n --argfile f1 file1.json --argfile f2 file2.json -f program.jq

where program.jq is:

[$f1,$f2]
| transpose
| map(add)

posted this

Have an answer?

JD

Please login first before posting an answer.