json-diff
A lot of tools diff json, but only json-diff’s output is easily processable by a program.
In this post, we’ll explore json-diff (a tool from my json-toolkit), how to use it and how to write programs that use its output.
Using json-diff
json-diff is an easy-to-use CLI tool. It takes only two arguments: FILE1 and FILE2 which are json files.
Sample usage
$ echo '{"k": "v"}' > kv.json
$ echo '{"k": "o"}' > ko.json
$ json-diff kv.json ko.json
[{"leftValue":"v","path":["k"],"rightValue":"o"}]
We can also pretty print the output by piping the output to jq.
$ json-diff kv.json ko.json | jq
[
{
"leftValue": "o",
"path": [
"k"
],
"rightValue": "v"
}
]
json-diff doesn’t have a pretty printing option built in because jq does that for us.
Explanation of the output
The json-diff output is an array of “difference objects”. Each difference object has a:
path (required) -j path is an array of strings and numbers that describe where in the json the difference lies. Strings are for access…