Home > front end >  Compare two JSON file and output unmatched values from a file using Jq in Shell script
Compare two JSON file and output unmatched values from a file using Jq in Shell script

Time:09-17

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.

CodePudding user response:

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]'
  • Related