kk07
kk07

Reputation: 83

Compare two JSON file and output unmatched values from a file using Jq in Shell script

How can I output the objects from a JSON file which doesn't match in another JSON file. For Example,

file1:

[
{ 
  "name": "ABC",
  "age": "23",
  "address": "xyz"
},
{
  "name": "DEF",
  "age": "24",
  "address": "pqr"
}
]

file2:

[
{ 
  "name": "ABC",
  "age": "23",
  "address": "xyz"
},
{
  "name": "GHI",
  "age": "24",
  "address": "pqr"
}
]

I want the output from file2 which doesn't match in file1. Example:

output file:

[
{
  "name": "GHI",
  "age": "24",
  "address": "pqr"
}
]

I am looking to do it in shell script using Jq.

Upvotes: 1

Views: 2630

Answers (1)

Botje
Botje

Reputation: 31193

The jq minus operator does exactly what you need. Just use --slurpfile to get both files into variables:

jq -n --slurpfile file1 file1.json --slurpfile file2 file2.json '$file2[0] - $file1[0]'

Upvotes: 5

Related Questions