Skip to content

Comparing Two JSON Files ​

A plain diff on two JSON files reports every reordered key and whitespace change as a difference. Normalize both files with fx first, or let fx compute the differences itself. Install fx to follow along.

The examples use two versions of a service config, a.json:

json
{"name": "billing-api", "version": "1.4.0", "replicas": 2, "env": {"LOG_LEVEL": "info", "REGION": "eu-west-1"}, "ports": [8080, 9090]}

and b.json:

json
{
  "version": "1.5.0",
  "name": "billing-api",
  "env": {"REGION": "eu-west-1", "LOG_LEVEL": "debug"},
  "ports": [8080, 9090],
  "healthcheck": "/healthz"
}

Diff JSON Files Ignoring Key Order ​

Add a sortKeys function to .fxrc.js that sorts object keys at every level:

js
function sortKeys(x) {
  if (Array.isArray(x)) return x.map(sortKeys)
  if (x && typeof x === 'object') {
    return Object.fromEntries(
      Object.keys(x).sort().map(k => [k, sortKeys(x[k])])
    )
  }
  return x
}

Normalize both files, then diff them. --no-inline puts every array element on its own line, so a diff points at the exact element that changed:

bash
fx --no-inline a.json sortKeys > a.sorted.json
fx --no-inline b.json sortKeys > b.sorted.json
diff -u a.sorted.json b.sorted.json

Output:

diff
--- a.sorted.json	2026-09-26 15:54:31
+++ b.sorted.json	2026-09-26 15:54:32
@@ -1,13 +1,13 @@
 {
   "env": {
-    "LOG_LEVEL": "info",
+    "LOG_LEVEL": "debug",
     "REGION": "eu-west-1"
   },
+  "healthcheck": "/healthz",
   "name": "billing-api",
   "ports": [
     8080,
     9090
   ],
-  "replicas": 2,
-  "version": "1.4.0"
+  "version": "1.5.0"
 }

Without .fxrc.js, pass the same logic inline using walk:

bash
fx --no-inline a.json 'walk(x => x && typeof x === "object" && !Array.isArray(x) ? Object.fromEntries(Object.entries(x).sort(([a], [b]) => a < b ? -1 : 1)) : x)'

Diff JSON Without Temporary Files ​

In bash or zsh, use process substitution to skip the intermediate files:

bash
diff <(fx --no-inline a.json sortKeys) <(fx --no-inline b.json sortKeys)

This reports the same changes as above in diff's default format, and leaves no .sorted.json files on disk. Add -u for the unified format.

List Keys Added, Removed, or Changed ​

Slurp both files into one array with -s and compare the top-level keys:

bash
cat a.json b.json | fx -s '([a, b]) => ({
  added: Object.keys(b).filter(k => !(k in a)),
  removed: Object.keys(a).filter(k => !(k in b)),
  changed: Object.keys(a).filter(k => k in b && JSON.stringify(a[k]) !== JSON.stringify(b[k])),
})'

Output:

json
{
  "added": [ "healthcheck" ],
  "removed": [ "replicas" ],
  "changed": [
    "version",
    "env"
  ]
}

Show Every Changed Path in Nested JSON ​

Flatten each document into path: value pairs, then compare the pairs. Nested keys and array indexes become dotted paths, and key order no longer matters:

bash
cat a.json b.json | fx -s '([a, b]) => {
  const flat = (x, p = "") => x && typeof x === "object"
    ? Object.entries(x).flatMap(([k, v]) => flat(v, p ? p + "." + k : k))
    : [[p, x]];
  const A = Object.fromEntries(flat(a)), B = Object.fromEntries(flat(b));
  return Object.keys({...A, ...B})
    .filter(k => A[k] !== B[k])
    .map(k => `${k}: ${JSON.stringify(A[k])} -> ${JSON.stringify(B[k])}`);
}' list

Output:

version: "1.4.0" -> "1.5.0"
replicas: 2 -> undefined
env.LOG_LEVEL: "info" -> "debug"
healthcheck: undefined -> "/healthz"

undefined means the path does not exist in that file.

Compare Two Arrays of Records by ID ​

A line diff gets confused when records are inserted, removed, or reordered. Index the records by id instead. The example ignores updatedAt when looking for modified records.

users-old.json:

json
[
  {"id": 1, "name": "Alice", "role": "admin", "updatedAt": "2024-05-01T10:00:00Z"},
  {"id": 2, "name": "Bob", "role": "viewer", "updatedAt": "2024-05-01T10:00:00Z"},
  {"id": 3, "name": "Carol", "role": "editor", "updatedAt": "2024-05-01T10:00:00Z"},
  {"id": 4, "name": "Dave", "role": "viewer", "updatedAt": "2024-05-01T10:00:00Z"}
]

users-new.json:

json
[
  {"id": 1, "name": "Alice", "role": "admin", "updatedAt": "2024-06-12T08:30:00Z"},
  {"id": 3, "name": "Carol", "role": "admin", "updatedAt": "2024-06-12T08:30:00Z"},
  {"id": 4, "name": "Dave", "role": "viewer", "updatedAt": "2024-06-12T08:30:00Z"},
  {"id": 5, "name": "Erin", "role": "editor", "updatedAt": "2024-06-12T08:30:00Z"}
]
bash
cat users-old.json users-new.json | fx -s '([a, b]) => {
  const oldById = new Map(a.map(u => [u.id, u]));
  const newById = new Map(b.map(u => [u.id, u]));
  const strip = ({updatedAt, ...rest}) => JSON.stringify(rest);
  return {
    added: b.filter(u => !oldById.has(u.id)).map(u => u.id),
    removed: a.filter(u => !newById.has(u.id)).map(u => u.id),
    modified: b.filter(u => oldById.has(u.id) && strip(u) !== strip(oldById.get(u.id))).map(u => u.id),
  };
}'

Output:

json
{
  "added": [ 5 ],
  "removed": [ 2 ],
  "modified": [ 3 ]
}

Ignore Timestamps and Other Volatile Fields ​

updatedAt changed on every record, so a raw diff marks all of them as changed. Drop the field and print one record per line with list:

bash
diff \
  <(fx users-old.json '.map(({updatedAt, ...u}) => JSON.stringify(u))' list) \
  <(fx users-new.json '.map(({updatedAt, ...u}) => JSON.stringify(u))' list)

Output:

2,3c2
< {"id":2,"name":"Bob","role":"viewer"}
< {"id":3,"name":"Carol","role":"editor"}
---
> {"id":3,"name":"Carol","role":"admin"}
4a4
> {"id":5,"name":"Erin","role":"editor"}

Check If Two JSON Files Are Equal in a Script ​

cmp -s prints nothing. It exits with 0 when the inputs match and 1 when they differ. Normalize the files first so that key order and formatting are ignored. c.json has the same data as a.json with its keys in a different order:

bash
if cmp -s <(fx sortKeys < a.json) <(fx sortKeys < c.json); then echo "same"; else echo "different"; fi

This prints same. diff -q returns the same exit codes but also prints a message when the files differ. To get the answer as JSON instead, compare inside fx:

bash
cat a.json c.json | fx -s sortKeys '([a, b]) => JSON.stringify(a) === JSON.stringify(b)'

Output:

true

See Also ​