0

I wish to match 2 json files based on common id I've tried using awk, jq and the npm json package in quite a lot of different ways but nothing have gotten close to working. The 2 json files are not sorted and do not contain all the same entries. they contain the common networkId, I only want the output to contain the entries from file2. Hope somebody can help! Here's an example.

file1.json:

[
  {
    "customerId": "id1",
    "networkId": "L_653021545945744804"
  },
  {
    "customerId": "id2",
    "networkId": "L_653021545955724805"
  },
  {
    "customerId": "id3",
    "networkId": "L_655051945958724557"
  },
  {
    "customerId": "id4",
    "networkId": "L_655567989968735408"
  }
]

file2.json:

[
  {
    "name": "a",
    "networkId": "L_653021545945744804"
  },
  {
    "name": "b",
    "networkId": "L_655051945958724557"
  }
]

Wanted output:

[
  {
    "customerId": "id1",
    "name": "a",
    "networkId": "L_653021545945744804"
  },
  {
    "customerId": "id3",
    "name": "b",
    "networkId": "L_655051945958724557"
  }
]
jdagrofa
  • 3
  • 1

1 Answers1

1

This is a task for INDEX, JOIN and add:

jq '[JOIN(INDEX(.networkId); input[]; .networkId; add)]' file1.json file2.json
[
  {
    "name": "a",
    "networkId": "L_653021545945744804",
    "customerId": "id1"
  },
  {
    "name": "b",
    "networkId": "L_655051945958724557",
    "customerId": "id3"
  }
]

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31
  • upvoted, because this solves my similar problem where I want to include the recs from the 1st set that don't have a corresponding id in the second set: jq '[JOIN(INDEX(input[]; .id); .[]; .id; add)]' [Demo](https://jqplay.org/s/V7Yxs8Svw9) – jamacoe May 04 '22 at 10:10
  • remark: Oracle Linux 8 with EPEL 8 installed didn't support jq 1.6 which seems to be necessary for JOIN(). The upgrade wasn't trivial and I managed it following this advice: [Upgrading jq to 1.5 on Ubuntu](https://stackoverflow.com/a/42622937/4335480) – jamacoe May 05 '22 at 07:04