JSON Diff

Produce a minimal deterministic RFC 6902-style add/remove/replace patch turning one JSON document into another.

Description

Compare two JSON documents and generate a deterministic add, remove, and replace patch.

JSON diff turns two serialized JSON values into a compact machine-readable description of their differences. It is useful for API regression fixtures, configuration reviews, and producing a patch that can be passed directly to the JSON Patch applier.1, 2

When to use JSON Diff

  • Compare before-and-after API responses
  • Create a replayable change set for JSON fixtures
  • Locate nested object and array differences with JSON Pointer paths

How the calculation works

Objects are compared by sorted keys, arrays are compared positionally, and scalar or type changes produce `replace`. Removed trailing array items are emitted from the highest index downward so later indices remain valid. Paths escape `/` and `~` according to JSON Pointer.1, 2

Interpreting the result

`opCount` gives the patch length and `patch` contains compact JSON. An empty array means the parsed documents are structurally equal. The generated operations are deterministic and round-trip through the companion applier, but they are not guaranteed to be the globally shortest possible edit script.

Important limitations

  • Array items are matched by position rather than by identity or key.
  • Different object key order is ignored because JSON objects are compared structurally.
  • The output uses only add, remove, and replace operations.

References

  1. RFC 6902: JavaScript Object Notation (JSON) Patch — RFC Editor

  2. RFC 6901: JavaScript Object Notation (JSON) Pointer — RFC Editor

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies