I have two files containing json arrays like so
file1.json
[
{
"classid":"abc",
"name":"Alex"
},
{
"classid":"abc",
"name":"Bob"
}
]
file2.json
[
{
"classid":"abc",
"classname":"math"
}
]
I'd like to combine these two files on "classid". Desired output.json
[
{
"classid":"abc",
"name":"Alex",
"classname":"math
},
{
"classid":"abc",
"name":"Bob",
"classname":"math
}
]
I tried
> jq -s 'reduce .[] as $item ({}; . *= $item)' file1.json file2.json
but I see a jq: error (at file1.json:10): object ({}) and array ([{"classid...) cannot be multiplied
Would appreciate any help.
CodePudding user response:
If you have jq 1.6, just use JOIN
with an INDEX
:
jq '[JOIN(INDEX(input[]; .classid); .[]; .classid; add)]' file1.json file2.json
[
{
"classid": "abc",
"name": "Alex",
"classname": "math"
},
{
"classid": "abc",
"name": "Bob",
"classname": "math"
}
]