Unified Diffs and Patches: Create, Summarize, and Apply Changes Cleanly
How unified diffs work, when to use a patch viewer vs text/JSON diff, and a safe browser workflow for creating and applying single-file patches.
How to compare JSON structurally — key order, arrays, nested objects — and when to use JSON diff versus line-based text diff in config and API review workflows.
By codefunc
Line-based diffs treat JSON as plain text. That works until key order changes, whitespace shifts, or an array is reordered without semantic change — then the PR lights up red for no product reason. Structural JSON diff compares objects and arrays as data: keys matter, formatting does not, and you can see that "retries" moved from 3 to 5 without noise from indentation. Choosing the right diff style speeds reviews and prevents rubber-stamping real changes hidden in formatting churn.
Bottom line: Format first, then structural-diff JSON for config and API payloads. Use text diff for intentional prose or when byte-identical output matters. Never assume key order in objects is meaningful unless your consumer documents it.
Developers reviewing infrastructure config, feature-flag JSON, OpenAPI examples, localization files, and API fixture updates. Also useful for incident responders comparing "before" and "after" webhook bodies.
| Aspect | Text (line) diff | Structural JSON diff |
|---|---|---|
| Input model | Lines of text | Parsed trees |
| Key reorder | Noisy (delete + add) | Usually no change |
| Indent / spacing | Noisy | Ignored after parse |
| Array reorder | Looks like many edits | Reported as moves or replace, depending on tool |
| Invalid JSON | Still "diffs" | Must parse first |
| Partial lines | Shows exact characters | Shows path-based changes (a.b[2].c) |
Example — same object, different key order:
{"b": 2, "a": 1}
{"a": 1, "b": 2}
git diff may show a full replacement. A JSON diff should report no semantic change.
RFC 8259 says object key order is not significant. In practice:
JSON.stringify in JavaScript enumerates string keys in insertion orderIf your pipeline re-serializes config on every save, text diffs become unreadable. Stabilize output:
# Sort keys for a canonical text form (jq)
jq -S . config.json > config.sorted.json
Or skip canonicalization and compare with structural JSON diff so reviews focus on values.
Unlike objects, array order is significant in JSON. [1, 2] is not [2, 1].
| Array role | Order matters? | Diff expectation |
|---|---|---|
| List of steps / pipeline stages | Yes | Reorder = real change |
| Set of tags stored as array | Often no (app-defined) | Consider sorting before compare |
| Table rows | Yes | Index-based diffs |
| Patch lists | Yes | Positional |
Structural tools typically express array edits as index paths:
/items/2/price: 9.99 → 12.50
/items/5: removed
/items: + { "sku": "SKU-9", "qty": 1 }
When an array is a logical set, sort it in the formatter step before either text or structural compare so membership changes stay obvious.
Paste both sides into the JSON formatter with the same indent (2 spaces). Invalid JSON fails here — fix syntax before debating semantics. Pair with a quick text diff only if you care about exact formatting (e.g. golden files).
Run the formatted outputs through JSON diff. Read path-based changes:
string → number)| Diff result | Action |
|---|---|
| Values changed as expected | Approve |
Type changed ("10" → 10) |
Check consumers and schemas |
| Keys removed | Confirm backward compatibility |
| Only key order / whitespace | Prefer structural tools next time; no product change |
| Huge replace of a large array | Ask for a smaller fixture or explain migration |
For generated JSON (export from a UI, Terraform plan JSON, etc.), either:
{
"flags": {
"checkout_v2": { "enabled": true, "rollout": 0.1 },
"search_boost": { "enabled": false }
}
}
A structural diff highlighting rollout: 0.1 → 0.5 is reviewable. A 40-line text diff caused by key sorting is not.
JSON/YAML configs often get reformatted by editors. Parse-and-diff (or YAML-aware structural tools) prevents "format-only" PRs from hiding a real image tag change. When you only have JSON, format + JSON diff is the fast path.
When updating expected responses:
Use both: structural for meaning, text for representation.
Comparing production config export vs last known good:
Client-side tools keep exports on your machine — prefer that when configs contain internal hosts or tokens. codefunc's JSON diff, text diff, and JSON formatter run in the browser with no upload step for the payload.
1.0 vs 1) depends on the comparatorjq, dyff, json-diff npm) than paste UIsReview JSON as structure when you care about behavior; review it as text when you care about exact bytes or surrounding prose. Format with the JSON formatter, compare meaning with JSON diff, and fall back to text diff for line-oriented or non-JSON content. Treat object key order as noise, array order as signal, and type changes as potential breaks — that is how config and fixture reviews stay short and honest.
How unified diffs work, when to use a patch viewer vs text/JSON diff, and a safe browser workflow for creating and applying single-file patches.
When to use JSON, YAML, or TOML for app config — conversion pitfalls, trailing commas, multiline strings, and a safe workflow for editing and validating files.
How to convert between CSV and JSON safely — quoting, nested objects, type coercion, headers, and a workflow that catches breakage before it hits production.
Find a developer tool