How to Compare JSON Files Without a Library

Walk both structures yourself when the dependency would be overkill, and use the browser tool when reviewing the diff visually is faster.

Bringing in DeepDiff or jsondiffpatch for a single comparison feels heavy. The standard library can give you a path-level diff in about 30 lines, and that is often all you need.

1. JavaScript: walk both objects recursively

function diff(a, b, path = '') { const out = []; const keys = new Set([...Object.keys(a || {}), ...Object.keys(b || {})]); for (const key of keys) { const next = path ? `${path}.${key}` : key; if (!(key in a)) out.push({ path: next, type: 'added', value: b[key] }); else if (!(key in b)) out.push({ path: next, type: 'removed', value: a[key] }); else if (typeof a[key] === 'object' && a[key] !== null && typeof b[key] === 'object' && b[key] !== null) { out.push(...diff(a[key], b[key], next)); } else if (a[key] !== b[key]) { out.push({ path: next, type: 'modified', from: a[key], to: b[key] }); } } return out;
}

This produces a flat list of {path, type, value} entries, which is easy to log, snapshot, or feed into a test assertion.

2. Python: same shape with json and recursion

def diff(a, b, path=''): out = [] keys = set((a or {}).keys()) | set((b or {}).keys()) for key in keys: nxt = f'{path}.{key}' if path else key if key not in a: out.append((nxt, 'added', b[key])) elif key not in b: out.append((nxt, 'removed', a[key])) elif isinstance(a[key], dict) and isinstance(b[key], dict): out.extend(diff(a[key], b[key], nxt)) elif a[key] != b[key]: out.append((nxt, 'modified', a[key], b[key])) return out

3. Handle arrays the way your data needs

The recursive walks above only descend into dicts and objects. Arrays are compared with !==. If you need positional array diffs, replace the array branch with an indexOf walk; if you need set-style array comparison, sort both sides before comparing. There is no one-size-fits-all answer because arrays mean different things in different schemas.

When to switch to the browser tool

If you only have two JSON files and a human is going to read the diff, writing this is overkill. Open JSON Diff, paste both sides, and you have a side-by-side view in seconds. For API responses, use API Response Diff; for package files, package.json Diff.

For the broader topic, browse the JSON Comparison Tools hub.