Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding all elements in a JSON dictionary which are not part of another dictionary with jq

I have to JSON files.

a.json:

[ 
  { key1: "foo", key2: "bar"},
  { key1: "foo", key2: "baz"},
  { key1: "bla", key2: "blubb"},
]

b.json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ 
  { key1: "foo", key2: "bar"},
  { key1: "foo", key2: "oof"},
  { key1: "bla", key2: "bla"},
]

My desired result is an array containing all the entrise of the first dictionary which are not part of the second one (equivalent to what the DISTINCT operator would do in SQL). So the expected output would be:

[ 
  { key1: "foo", key2: "baz"},
  { key1: "bla", key2: "blubb"},
]

How can I achieve this using jq?

>Solution :

Can’t find a valid duplicate, so posting this as an answer.


If you use -s (slurp), you can use object1 - object2 to get the difference between them:

jq -s  '.[0] - .[1]' a b

Will output:

[
  {
    "key1": "foo",
    "key2": "baz"
  },
  {
    "key1": "bla",
    "key2": "blubb"
  }
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading