JSON Diff & Patch API

Compare two JSON documents structurally and get a JSON Patch (RFC 6902) and Merge Patch (RFC 7386); apply patches to a document.

POSThttps://apibox.io.vn/v1/json-diff

The JSON diff API compares two JSON values by structure, not by text: key order and formatting do not matter. action=diff (with a and b) returns whether they are equal, a summary of added, removed and replaced values, an RFC 6902 JSON Patch that turns a into b and the equivalent RFC 7386 Merge Patch, the format used by many REST APIs for PATCH requests.

action=patch applies a JSON Patch (add, remove, replace, move, copy, test) to a document and returns the result; a failing test or a missing path returns 422 with the index of the operation, so patches can be validated before they reach your database. action=merge applies a Merge Patch (null removes a key).

Arrays are compared after removing their common beginning and end, so appending or removing items produces short patches. Paths use JSON Pointer with ~0 / ~1 escaping.

Parameters

NameInTypeRequiredDefault
actionbodystringnodiffdiff, patch or merge.
abodyobjectnoFor diff: the original JSON.
bbodyobjectnoFor diff: the new JSON.
jsonbodyobjectnoFor patch / merge: the document.
patchbodyarraynoRFC 6902 operations.
merge_patchbodyobjectnoRFC 7386 merge patch.

Example

curl -X POST "https://apibox.io.vn/v1/json-diff" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"a":{"name":"A","tags":["x"],"n":{"a":1,"b":2}},"b":{"name":"B","tags":["x","y"],"n":{"a":1}}}'

Response

{
  "equal": false,
  "summary": {
    "added": 1,
    "removed": 1,
    "replaced": 1
  },
  "patch": [
    {
      "op": "replace",
      "path": "/name",
      "value": "B"
    },
    {
      "op": "add",
      "path": "/tags/1",
      "value": "y"
    },
    {
      "op": "remove",
      "path": "/n/b"
    }
  ],
  "merge_patch": {
    "name": "B",
    "tags": [
      "x",
      "y"
    ],
    "n": {
      "b": null
    }
  }
}

Use cases

  • Show what changed between two API responses or config versions
  • Generate PATCH bodies from an edited object
  • Audit logs that store only the changes
  • Validate and apply patches sent by clients

FAQ

JSON Patch or Merge Patch?

JSON Patch is a list of precise operations (including array positions and tests). Merge Patch is a partial document: simpler, but it cannot set a value to null or change part of an array.

Does key order matter?

No. {"a":1,"b":2} equals {"b":2,"a":1}. Array order does matter.

What happens when a patch fails?

Nothing is applied; you get HTTP 422 with code patch_failed and the index of the failing operation.

Related APIs

Start with 1,000 free requests every month. Get free API key →