Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge arrays from two files into one array with jq?

Tags:

json

join

jq

I would like to merge two files containing JSON. They each contain an array of JSON objects.

registration.json

[
    { "name": "User1", "registration": "2009-04-18T21:55:40Z" },
    { "name": "User2", "registration": "2010-11-17T15:09:43Z" }
]

useredits.json

[
    { "name": "User1", "editcount": 164 },
    { "name": "User2", "editcount": 150 },
    { "name": "User3", "editcount": 10 }
]

In the ideal scenario, I would like to have the following as a result of the merge operation:

[
    { "name": "User1", "editcount": 164, "registration": "2009-04-18T21:55:40Z" },
    { "name": "User2", "editcount": 150, "registration": "2010-11-17T15:09:43Z" }
]

I have found https://github.com/stedolan/jq/issues/1247#issuecomment-348817802 but I get

jq: error: module not found: jq
like image 861
Smile4ever Avatar asked Feb 28 '18 20:02

Smile4ever


2 Answers

jq solution:

jq -s '[ .[0] + .[1] | group_by(.name)[] 
          | select(length > 1) | add ]' registration.json useredits.json

The output:

[
  {
    "name": "User1",
    "registration": "2009-04-18T21:55:40Z",
    "editcount": 164
  },
  {
    "name": "User2",
    "registration": "2010-11-17T15:09:43Z",
    "editcount": 150
  }
]
like image 114
RomanPerekhrest Avatar answered Sep 26 '22 00:09

RomanPerekhrest


Although not strictly answering the question, the command below

jq -s 'flatten | group_by(.name) | map(reduce .[] as $x ({}; . * $x))'
      registration.json useredits.json

generates this output:

[
    { "name": "User1", "editcount": 164, "registration": "2009-04-18T21:55:40Z" },
    { "name": "User2", "editcount": 150, "registration": "2010-11-17T15:09:43Z" },
    { "name": "User3", "editcount": 10 }
]

Source: jq - error when merging two JSON files "cannot be multiplied"

like image 23
Smile4ever Avatar answered Sep 26 '22 00:09

Smile4ever