Home > Blockchain >  Collapse distinct JSON objects into one with jq
Collapse distinct JSON objects into one with jq

Time:12-09

I have a set of JSON objects that each contain one unique key:value pair (that is, there are no duplicate keys). I want to combine all of these into a single object using jq.

I want this:

{
  "Key1": "Value1"
}
{
  "Key2": "Value2"
}
{
  "Key3": "Value3"
}
...

to become this:

{
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value3",
  ...
}

CodePudding user response:

Since this is a single file, use slurp and simply add the objects:

jq -s 'add' yourfile

Output:

{
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value3"
}

slurp reads all entities in the input file as one big array, which will then be reduced with add. Adding two objects simply merges their key-value pairs, with conflicting keys being overwritten by the last value.

If your input is really really large, you might not want to load all entities into memory at once and you might want to reduce on inputs instead:

jq -n 'reduce inputs as $obj ({}; .   $obj)' yourfile

You might find the question Difference between slurp, null input, and inputs filter helpful.

  • Related