skip to Main Content

Imagine I have two files as below:

file1:

[   
  "id1",
  "id2"
]

file2:

[
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id1",
    "name": "name2"
  },
  {
    "id": "id3",
    "name": "name3"
  },
  {
    "id": "id5",
    "name": "name5"
  },
  {
    "id": "id2",
    "name": "name6"
  }
]

I want to find those entries in file2 that their id exist in file1, in the example above the expected output should be like:

[
  {
    "id": "id1",
    "name": "name1"
  },
  {
    "id": "id1",
    "name": "name2"
  },
  {
    "id": "id2",
    "name": "name6"
  }
]

How can I achieve it with jq.

2

Answers


  1. You’ll get better/more succinct answers, but this seems to work. Slurp both files into an array, use file1.json contents as a filter to reduce file2.json.

    jq --slurp '.[0] as $filt | reduce .[1][] as $ref ([]; if IN($ref.id; $filt[]) then . + [$ref] else . end)' file1.json file2.json
    

    Try it on jqplay.org.

    If the file sizes are large, this might be better since it doesn’t slurp.

    jq --null-input 'input as $filt | reduce inputs[] as $ref ([]; if IN($ref.id; $filt[]) then . + [$ref] else . end)' file1.json file2.json
    

    Try this on jqplay.org.

    Both output:

    [
      {
        "id": "id1",
        "name": "name1"
      },
      {
        "id": "id1",
        "name": "name2"
      },
      {
        "id": "id2",
        "name": "name6"
      }
    ]
    
    Login or Signup to reply.
  2. With both inputs already being arrays, there’s no need to --slurp. Also, as there are exactly two inputs, there’s also no need to call on --null-input, just use input for the second one. For example, using the inputs in reversed order:

    jq 'input as $ids | map(select(IN(.id; $ids[])))' file2.json file1.json
    
    [
      {
        "id": "id1",
        "name": "name1"
      },
      {
        "id": "id1",
        "name": "name2"
      },
      {
        "id": "id2",
        "name": "name6"
      }
    ]
    

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search