Use jq to first sort all keys. Then diff to do the diffing:
jq -S . A.json > A-sorted.json
jq -S . B.json > B-sorted.json
diff A-sorted.json B-sorted.json
The above example is for Linux but both jq and diff is available for Windows.
It seems Windows has an alternative diff tool called fc, perhaps this could be used instead of diff.
Use jq to first sort all keys. Then diff to do the diffing:
jq -S . A.json > A-sorted.json
jq -S . B.json > B-sorted.json
diff A-sorted.json B-sorted.json
The above example is for Linux but both jq and diff is available for Windows.
It seems Windows has an alternative diff tool called fc, perhaps this could be used instead of diff.
QT JSON Diff [Github] might be for you:
- They have a Windows build (see "Win64" in the releases)
- It is gratis, MIT license
- It works offline, as an EXE which runs locally
- can sort objects inside arrays so that order matters less
diff - Using jq or alternative command line tools to compare JSON files - Stack Overflow
Comparing two json files : shell scripting - Stack Overflow
Compare json files
command line - CLI tool to compare 2 JSON files without regards to order of data objects AND returns an errorlevel environment variable - Software Recommendations Stack Exchange
If your shell supports process substitution (Bash-style follows, see docs):
diff <(jq --sort-keys . A.json) <(jq --sort-keys . B.json)
Objects key order will be ignored, but array order will still matter. It is possible to work-around that, if desired, by sorting array values in some other way, or making them set-like (e.g. ["foo", "bar"] → {"foo": null, "bar": null}; this will also remove duplicates).
Alternatively, substitute diff for some other comparator, e.g. cmp, colordiff, or vimdiff, depending on your needs. If all you want is a yes or no answer, consider using cmp and passing --compact-output to jq to not format the output for a potential small performance increase.
Use jd with the -set option:
No output means no difference.
$ jd -set A.json B.json
Differences are shown as an @ path and + or -.
$ jd -set A.json C.json
@ ["People",{}]
+ "Carla"
The output diffs can also be used as patch files with the -p option.
$ jd -set -o patch A.json C.json; jd -set -p patch B.json
{"City":"Boston","People":["John","Carla","Bryan"],"State":"MA"}
https://github.com/josephburnett/jd#command-line-usage
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
Just use diff. Like in
diff --unified file1.json file2.json
Say I have 2 json files.
#1
{
"ID": 1,
"Value": "A"
}
{
"ID": 2,
"Value": "B",
}
#2
{
"ID": 2,
"Value": "B"
},
{
"ID": 1,
"Value": "A",
}
I would like a compare that says these 2 files are the same.
Just because they are not in the same order they are not different.
Are there any tools that will tell me they are the same?
thanks
You can run both files through jq (available as a small Windows binary), let jq pretty-print and sort the data in both files according to your needs, and then perform an "ordinary" diff (with WinDiff, Meld, Diffuse).
the shell commands would be
< file1.json jq 'keys' > file1.sorted.json
< file2.json jq 'keys' > file2.sorted.json
diff file1.sorted.json file2.sorted.json
# or better
diff -q file1.sorted.json file2.sorted.json
#
# -q, --brief
# report only when files differ
# if output of diff -q is nonempty, files differ -> raise an error
If you know what's going on, continue with the unmodified files.
This obviously works only if the JSON objects differ only in their keys. This also assumes that the type info is preserved (e.g. the string representation of floats does not differ between the two files)
If the JSON objects differ in object values, or if the values contain nested objects (which themselves can differ in values or in subkey ordering), or if you want the order of the first-level-keys unchanged, but need to only compare the nested "value-objects" by some other criterion, you will need more complicated jq commands.
Check stackoverflow.com - some incredible jq experts there.
Did you try and take a look at GNU Diffutils (diff)? Diff specifically has the "-B" or "--ignore-blank-lines" switch, which "Ignore changes whose lines are all blank" (see also a similar question on Stack Exchange).
There are other ways and variants as well to achieve the things you mentioned, such as using Vimdiff for example.
If you want to take a programmatic approach, you might want to take a look at the Levenshtein distance, which is a metric for measuring the difference between two Strings.