6

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"
  }
}
peak
  • 105,803
  • 17
  • 152
  • 177
JdeHaan
  • 332
  • 6
  • 19

1 Answers1

9

Let's assume you have jq 1.5 or later, and that your JSON objects are in one or more files. Then for your first expected output you can simply write:

[inputs | .a | { (.b): .d} ] | add

This assumes you use the -n command-line option. I'm sure that once you see how simple the solution is, further explanation will be superfluous.

For your second expected output, you can just wrap the above line in:

{x: _}

E.g.:

$ jq -ncf program.jq input.json
{"x":{"c":"e","f":"g"}}

p.s. Your approach using reduce is fine, but you'd need to use the -s command-line option.

p.p.s. Do you owe me another beer?

peak
  • 105,803
  • 17
  • 152
  • 177
  • You're close, closer than I have been until now, I have to admit that, but the input is a single file :-( (PS. Are going for a full crate? ;-) ) – JdeHaan Jul 24 '18 at 07:26
  • As I said, the solution with `inputs` will work no matter how many files there are. – peak Jul 24 '18 at 07:28
  • Nope, the ';' gives a syntax error (am on linux). Leaving it out gives { "f": "g" }, not { "c": "e", "f": "g" } :-( – JdeHaan Jul 24 '18 at 07:32
  • The ; was a typo. Fixed. – peak Jul 24 '18 at 07:40
  • Leaving out the input, the result still is | jq '[inputs | .a | { (.b): .d} ] | add' { "f": "g" } not { "c": "e", "f": "g" } – JdeHaan Jul 24 '18 at 07:42