7 min read
How to compare two JSON files
A practical guide to comparing JSON payloads, configs, arrays, and API responses without getting distracted by formatting.
Table of contents
Prepare both files
Start by validating both JSON files. If either file is invalid, fix syntax first. Then format both files so you can read the structure. Formatting is not required for parsed comparison, but it helps you understand the diff output.
If the files are very large, isolate the object or array you care about. Comparing smaller sections often produces clearer results.
Compare values, not whitespace
A useful JSON diff compares parsed values rather than raw text. Whitespace, indentation, and line breaks should not count as meaningful differences. A changed boolean, missing key, new array item, or different string should count.
This distinction matters because two JSON files can look different but represent the same data. Formatting both with the same tool can remove visual noise before deeper comparison.
Understand array differences
Arrays are ordered. If item order matters, comparing by index is correct. If order does not matter, a simple diff can be noisy. For arrays of objects, it may be better to sort by id or compare specific keys.
When a diff reports many changes after one insertion, check whether an array item shifted positions. The data may not be as different as the output first suggests.
Good JSON comparison use cases
JSON comparison is useful for API regression checks, config reviews, feature flag debugging, webhook version analysis, and test fixture updates. A short list of changed paths is often easier to share than two full payloads.
After finding a difference, decide whether it is expected. Not every difference is a bug; timestamps, ids, ordering, and generated values may change naturally.
Related guides
What is JSON and why developers use it
A practical explanation of JSON, where it appears in development, and why its simple structure made it the common language of APIs.
How to format JSON online
Learn when to format JSON, how online formatters work, and what to check when formatting fails.
How to validate JSON and fix common errors
A practical guide to JSON validation, parser messages, and the most common syntax mistakes developers run into.
FAQ
Should JSON be formatted before comparing?
Formatting helps humans read the files, but a parsed JSON diff should ignore whitespace.
Why do reordered arrays show many changes?
Many diff tools compare arrays by index, so moving items can appear as multiple value changes.
Can invalid JSON be compared?
No. Both files should parse successfully before value comparison.
What does a JSON path mean?
A JSON path points to a nested location, such as $.user.name or $.items[0].id.