How to compare JSON files offline
At work, I needed to compare some huge (almost identical) JSON data structures.
Since the data was potentially sensitive, I wanted to do it offline. Sending data over the wire to one of the myriad of online services was a no-go.
There are probably built-in tools for IDE’s like VS Code, Emacs and the like, but I wanted something I could use on the Linux CLI
First I looked for a single tool. I read someone referencing a tool in Python, but the guides I found deterred me due to the “many” steps and dependencies.
I also stumbled over a Windows user, asking for advice on how to diff JSON on StackExchange.
When I by accident realized I could get jq
to sort all the keys in a JSON file, I decided to combine jq
and diff
to solve my problem.
jq -S . A.json > A-sorted.json
jq -S . B.json > B-sorted.json
diff A-sorted.json B-sorted.json
Since I already had both tools installed and I use both tools regularly, it seemed like the best solution for me.
How to compare JSON files offline
© 2024 by Jacob Emcken is licensed under CC BY-SA 4.0