Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two json files : shell scripting

Tags:

json

shell

diff

jq

I would like to compare two json files which look like the following:

[
   {
      "type" : 1,
      "children" : {
         "nsubj" : {
            "role" : "topic",
            "POS" : [
               "noun"
            ]
         }
      },
      "role" : "vehicle",
      "POS" : [
         "noun"
      ]
   },

and the other is in the similar format, but there are some differences between the two because one json file is made up of 3336 lines, while another is made up of 3724 lines. I would like to write a shell script which would compare the two line by line and whenever it finds a difference, output the line number where the difference occurred.

like image 597
gkumar7 Avatar asked Dec 31 '13 01:12

gkumar7


People also ask

How do you compare two JSON files?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.


3 Answers

Just use diff. Like in

diff --unified file1.json file2.json
like image 51
Sergii Dymchenko Avatar answered Sep 24 '22 17:09

Sergii Dymchenko


Just to update on the answer from bartolomeon_n, you can actually do this all on one line.

diff <(jq -S . fileA.json) <(jq -S . fileB.json)
# or, with nice columns and colours:
diff -y --left-column --color <(jq -S . fileA.json) <(jq -S . fileB.json)
like image 42
csaroff Avatar answered Sep 22 '22 17:09

csaroff


To compare json files you should convert them so they have same order of keys. Very good tool for this job is jq (https://stedolan.github.io/jq/) where you can do:

jq -S . fileA.json > fileA_fmt.json
jq -S . fileB.json > fileB_fmt.json

then, you can use your favourite tool for text file comparison. I like kdiff3 for GUI or just plain diff when in pure command-line e.g.:

diff fileA_fmt.json fileB_fmt.json
like image 34
bartolomeon_n Avatar answered Sep 25 '22 17:09

bartolomeon_n