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.
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
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
action | body | string | no | diff | diff, patch or merge. |
a | body | object | no | For diff: the original JSON. | |
b | body | object | no | For diff: the new JSON. | |
json | body | object | no | For patch / merge: the document. | |
patch | body | array | no | RFC 6902 operations. | |
merge_patch | body | object | no | RFC 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}}}'const res = await fetch('https://apibox.io.vn/v1/json-diff', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"a": {
"name": "A",
"tags": [
"x"
],
"n": {
"a": 1,
"b": 2
}
},
"b": {
"name": "B",
"tags": [
"x",
"y"
],
"n": {
"a": 1
}
}
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/json-diff",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"a": {"name": "A", "tags": ["x"], "n": {"a": 1, "b": 2}}, "b": {"name": "B", "tags": ["x", "y"], "n": {"a": 1}}},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/json-diff');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"a":{"name":"A","tags":["x"],"n":{"a":1,"b":2}},"b":{"name":"B","tags":["x","y"],"n":{"a":1}}}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));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
JSON Validator
Check JSON syntax with exact line and column of errors, and validate data against a JSON Schema (draft 4, 7, 2019-09 or 2020-12).
{ }JSON Formatter
Pretty print JSON with 0-8 spaces or tabs, minify it, or sort object keys recursively. Syntax errors are reported with line and column.
CSVCSV to JSON
Convert CSV into an array of JSON objects using the header row, with delimiter auto-detection, number and boolean typing, and row-level errors.