Given the following input (which is a toned down version of the output with 100K+ objects of another complex query):
echo '{ "a": { "b":"c", "d":"e" } }{ "a": { "b":"f", "d":"g" } }' | jq '.'
{
"a": {
"b": "c",
"d": "e"
}
}
{
"a": {
"b": "f",
"d": "g"
}
}
desired output:
{
"c": "e",
"f": "g"
}
or (suits better for follow up usage):
{
x: {
"c": "e",
"f": "g"
}
}
I can't for the life of me figure out how to do it. My real problem of course is the multiple object input data, for which I really don't know whether it's valid JSON. Jq produces and accepts it, jshon does not. I tried various possibilities, but none of them produced what I need. I considered this the most likely candidate:
echo '{ "a": { "b":"c", "d":"e" } }{ "a": { "b":"f", "d":"g" } }' | jq ' . | { (.a.b): .a.d }'
{
"c": "e"
}
{
"f": "g"
}
But alas. Other things I tried:
' . | { x: { (.a.b): .a.d } }'
'{ x: {} | . | add }'
'{ x: {} | . | x += }'
'{ x: {} | x += . }'
'x: {} | .x += { (.a.b): .a.d }'
'{ x: {} } | .x += { (.a.b): .a.d }'
Another one, close, but no sigar:
'reduce { (.a.b): .a.d } as $item ({}; . + $item)'
{
"c": "e"
}
{
"f": "g"
}
Who cares to enlighten me?
So the full answer in the above use case, thanks to @peak, is
echo '{ "a": { "b": "c", "d": "e" } }{ "a": { "b": "f", "d": "g" } }' | jq -n '{ x: [inputs | .a | { (.b): .d} ] | add }'
{
"x": {
"c": "e",
"f": "g"
}
}