How to prevent git from generating false difference for JSON files?

2.3k views Asked by At

git thinks that two versions of a .json file is different, but actually their data is not:

@@ -469,9 +479,9 @@
       "integrity": "sha1-+GzWzvT1MAyOY+B6TVEvZfv/RTE=",
       "dev": true,
       "requires": {
+        "JSONStream": "1.3.1",
         "combine-source-map": "0.7.2",
         "defined": "1.0.0",
-        "JSONStream": "1.3.1",
         "through2": "2.0.3",
         "umd": "3.0.1"
       }

How can we tell git to ignore such differences for JSON files?

Note:

Following code from Tomasz Wegrzanowski's Blog:

echo "*.json diff=json" >> .gitattributes
git config diff.json.textconv json_pp

is not working and causing hangs on git diff

2

There are 2 answers

0
Radon8472 On

I thing you config hangs cause json_pp doesnt accept the filename as parameter. That means in your config json_pp waits for inputs on std-input stream, and cause you don`t deliver any input streams your git diff hangs In my gitconfig I have implemented json_pp like this:

[diff "json"]
         textconv = "cat \"$1\" | json_pp --json_opt=canonical,pretty" 

For me this works fine. If you like you can change the --json_opt values to have you favorite output-format. But if the order of keys change this config will still show a difference.

Tested in git-version 2.16.1.windows.1

0
CervEd On

git thinks that two versions of a .json file is different, but actually their data is not

They are different, textually different. This is the only way git versions files, by design.

How can we tell git to ignore such differences for JSON files?

You cannot. The link you refer to is only about transforming JSON files while viewing them using the git-diff tool.

The widely accepted practice to commit JSON and other similar configuration files (xml etc.) that are often machine generated is by enforcing a sort order. In your case it looks like you used a tool that did case-sensitive sorting whereas the file was originally sorted in case-insensitive manner.