I am new to JQ. I need to make a json file based on another 2 files. I am worked with it whole day and stack here. Badly need this.
Here is file 1
{
"name": "foo",
"key": "1",
"id": "x"
}
{
"name": "bar",
"key": "2",
"id": "x"
}
{
"name": "baz",
"key": "3",
"id": "y"
}
file 2
{
"name": "a",
"key": "1"
}
{
"name": "b",
"key": "1"
}
{
"name": "c",
"key": "2"
}
{
"name": "d",
"key": "2"
}
{
"name": "e",
"key": "3"
}
Expected Result:
{
"x": {
"foo": [
"a",
"b"
],
"bar": [
"c",
"d"
]
},
"y": {
"baz": [
"e"
]
}
}
I can do it with python script but I need it with jq.
Thanks in advance.
CodePudding user response:
Use reduce
on the first file's items ($i
) to successively build up the result object using setpath
with fields from the item and values as a matching map on the secondary dictionary file ($d
).
jq -s --slurpfile d file2 '
reduce .[] as $i ({}; setpath(
[$i.id, $i.name];
[$d[] | select(.key == $i.key).name]
))
' file1
CodePudding user response:
One advantage of the following solution is that only one of the two files is "slurped":
< file2 jq -nc --slurpfile file1 file1 '
(reduce inputs as {$name, $key} ({};
.[$key] = [$name])) as $dict
| reduce $file1[] as {$name, $key, $id} ({};
.[$id] = [ {($name): $dict[$key]} ] )
'