To format and compare JSON, pretty-print both files with a consistent indent and sorted keys, then run a structural diff (a text diff on the formatted output or a JSON-aware diff tool). Formatting alone makes differences visible; comparing structurally catches reordered keys and whitespace noise that a plain text diff would flag as changes. You need both files and either a text editor with a JSON formatter, a command-line tool like jq, or a browser-based JSON diff tool.
What You'll Need
- The two JSON files or snippets you want to compare (save them as
a.jsonandb.json). - A text editor that can format JSON (VS Code, Notepad++, Sublime Text, or any editor with a JSON formatter extension).
- Optional: a command-line environment with
jqinstalled, or Python 3 for a small script. - Optional: a browser-based JSON diff tool if you prefer not to install anything.
Step-by-Step
- Validate both files first. Open each file in your editor and check for syntax errors (missing commas, trailing commas, unquoted keys, single quotes). A JSON linter or the editor's built-in JSON validation will flag the line. If a file is invalid, formatting and diffing will produce misleading results, so fix syntax before anything else.
- Pretty-print each file. In VS Code, open the file and run Format Document (Shift+Alt+F on Windows/Linux, Shift+Option+F on macOS). In most editors, look for a "Format JSON" or "Beautify" command. The goal is consistent indentation (two or four spaces) and one key per line.
- Sort keys consistently. Key order does not matter in JSON, but it does matter to a text diff. If your formatter supports sorting keys alphabetically, enable it for both files. In
jq, usejq -S . a.json > a_sorted.jsonand the same forb.json. This prevents false differences caused only by key order. - Normalize whitespace and line endings. Save both files with the same line ending (LF or CRLF) and ensure there is no trailing whitespace. Many editors have a "Trim Trailing Whitespace" setting; enable it before saving.
- Run a text diff on the formatted files. Use your editor's compare feature (VS Code: right-click one file in Explorer and choose Select for Compare, then right-click the other and choose Compare with Selected), or run
diff -u a_sorted.json b_sorted.jsonin a terminal. Review the output: lines starting with-exist only in the first file, lines starting with+only in the second. - Use a JSON-aware diff for semantic comparison. If you need to ignore key order, array order, or numeric formatting (for example
1.0vs1), use a structural diff. In Python, load both files withjson.loadand compare the resulting objects; write a small recursive function or use a library likedeepdiffif installed. This tells you whether the data is equivalent, not just whether the text matches. - Check arrays carefully. Arrays are ordered in JSON, so
[1,2,3]and[3,2,1]are different. If order should not matter for your use case, sort the arrays before comparing (for example withjq 'sort'on each array) and document that decision so others reading the diff understand it. - Record the result. Note which keys were added, removed, or changed, and whether the change is a value change or a type change (string to number, object to array). Type changes are the ones that most often break downstream code, so call them out explicitly.
Common Mistakes
- Comparing unformatted files. A single missing newline or a different indent width makes every line look changed. Always format both sides first.
- Ignoring key order. Two JSON documents with the same data but different key order are equivalent, yet a plain text diff reports them as different. Sort keys before diffing.
- Treating array order as irrelevant. It is not. If your comparison assumes arrays are sets, you may miss a real change in ordering that matters to the consumer.
- Skipping validation. A file with a trailing comma may still load in a lenient parser but fail in a strict one. Validate with a strict parser so both files are held to the same standard.
- Comparing numbers as text.
1,1.0, and1e0are the same number but different text. A structural comparison handles this; a text diff does not. - Forgetting encoding. Files saved as UTF-8 with BOM can show a difference at the first character. Save both as UTF-8 without BOM.
Tips
- Keep a small script or editor snippet that formats, sorts keys, and diffs in one action; repeating the same three commands by hand invites inconsistency.
- When sharing a diff, include the formatted, key-sorted versions so the recipient sees the same output you did.
- For large files, diff a filtered subset first (for example only the keys you expect to change) to reduce noise.
- If you compare JSON from an API, capture the raw response to a file before formatting; formatting in place can hide the original byte-level differences you may need later.
Troubleshooting
- The diff shows everything as changed. Check line endings, indentation width, and key order. Re-save both files with the same formatter settings.
- The formatter refuses to run. The file is invalid JSON. Use the editor's error marker to find the line and fix the syntax (often a trailing comma or a missing quote).
- The structural comparison says they are equal but the text diff does not. That is expected: the data is equivalent but the text differs. Decide which answer you actually need and use the matching method.
- Numbers compare as different when they look the same. One side may be a string ("1") and the other a number (1). Check the type, not just the printed value.
- A tool reports a difference you cannot see. Look for invisible characters such as non-breaking spaces or a BOM. Re-save as plain UTF-8 and re-run.
Frequently Asked Questions
Does key order matter in JSON?
No. JSON objects are unordered by specification, so two objects with the same keys and values are equivalent regardless of order. Sort keys before a text diff to avoid false positives.
Does array order matter?
Yes. Arrays are ordered, so [1,2] and [2,1] are different values. Sort arrays only if your application treats them as sets, and say so explicitly.
Can I compare JSON without installing anything?
Yes. Many editors format and compare files built in, and browser-based JSON diff tools work without installation. For scripted or repeated comparisons, a command-line tool is more reliable.
Why do two identical-looking files diff as different?
Usually whitespace, line endings, key order, or a hidden character such as a BOM. Format both files with the same settings and re-save as UTF-8 without BOM.
What is the difference between a text diff and a structural diff?
A text diff compares characters and lines; a structural diff compares parsed data. Structural comparison ignores formatting and key order and reports semantic changes such as added keys or changed types.
How do I compare very large JSON files?
Filter to the relevant keys first, or use a tool that streams the file. Loading two multi-gigabyte files into memory at once can fail, so reduce the scope before comparing.