JSON Diff — PHP source
Compare two JSON documents structurally: added, removed and changed values reported at exact paths instead of raw text lines. Runs locally in your browser with a shareable link.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
// json-diff — structural JSON diff tool. PHP port (canonical TS:
// src/lib/json-diff.ts; Go twin: cli/json-diff). Both documents are parsed
// with json_decode (objects stay stdClass so {} and [] stay distinct), then
// walked in parallel — objects over the sorted union of both key sets (key
// order never matters), arrays by shared index with no LCS alignment (a
// shifted array reports every moved index as changed, by design). A type
// divergence collapses into ONE change at the diverging path, never
// per-child; equality is deep and order-insensitive, so same:true means the
// documents are structurally identical. Never throws — errors travel in the
// result array: "Left: …" / "Right: …" for parse failures, "Both inputs are
// required" for empty input.
/**
* TS deepEqual mirror: scalars strictly, arrays element-wise, stdClass
* objects by key set regardless of order. Booleans never equal numbers and
* strings never equal numbers — the guards avoid PHP's loose-comparison
* traps ("1" == 1, true == 1).
*/
function json_values_equal($a, $b): bool
{
if (is_bool($a) || is_bool($b)) {
return is_bool($a) && is_bool($b) && $a === $b;
}
if (is_int($a) || is_float($a) || is_int($b) || is_float($b)) {
return (is_int($a) || is_float($a)) && (is_int($b) || is_float($b)) && $a == $b;
}
if (is_string($a) || is_string($b)) {
return is_string($a) && is_string($b) && $a === $b;
}
if ($a === null || $b === null) {
return $a === null && $b === null;
}
if (is_array($a) && is_array($b)) {
if (count($a) !== count($b)) {
return false;
}
$b_values = array_values($b);
foreach (array_values($a) as $i => $av) {
if (!json_values_equal($av, $b_values[$i])) {
return false;
}
}
return true;
}
if ($a instanceof stdClass && $b instanceof stdClass) {
$ak = array_keys(get_object_vars($a));
$bk = array_keys(get_object_vars($b));
if (count($ak) !== count($bk)) {
return false;
}
foreach ($ak as $k) {
if (!in_array($k, $bk, true) || !json_values_equal($a->$k, $b->$k)) {
return false;
}
}
return true;
}
return false;
}
function json_diff_walk($left, $right, string $path, array &$out): void
{
if (json_values_equal($left, $right)) {
return;
}
if ($left instanceof stdClass && $right instanceof stdClass) {
$lk = array_keys(get_object_vars($left));
$rk = array_keys(get_object_vars($right));
$keys = array_values(array_unique(array_merge($lk, $rk)));
sort($keys, SORT_STRING);
foreach ($keys as $key) {
$child = "{$path}.{$key}";
$in_l = in_array($key, $lk, true);
$in_r = in_array($key, $rk, true);
if (!$in_l) {
$out[] = ['path' => $child, 'type' => 'added', 'after' => $right->$key];
} elseif (!$in_r) {
$out[] = ['path' => $child, 'type' => 'removed', 'before' => $left->$key];
} else {
json_diff_walk($left->$key, $right->$key, $child, $out);
}
}
return;
}
if (is_array($left) && is_array($right)) {
$shared = min(count($left), count($right));
$lv = array_values($left);
$rv = array_values($right);
for ($i = 0; $i < $shared; $i++) {
json_diff_walk($lv[$i], $rv[$i], "{$path}[{$i}]", $out);
}
for ($i = $shared; $i < count($rv); $i++) {
$out[] = ['path' => "{$path}[{$i}]", 'type' => 'added', 'after' => $rv[$i]];
}
for ($i = $shared; $i < count($lv); $i++) {
$out[] = ['path' => "{$path}[{$i}]", 'type' => 'removed', 'before' => $lv[$i]];
}
return;
}
$out[] = ['path' => $path, 'type' => 'changed', 'before' => $left, 'after' => $right];
}
/** Compare two JSON documents structurally. Both inputs are required. */
function json_diff(string $left_raw, string $right_raw): array
{
if (trim($left_raw) === '' || trim($right_raw) === '') {
return ['changes' => [], 'same' => false, 'error' => 'Both inputs are required'];
}
try {
$left = json_decode($left_raw, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return ['changes' => [], 'same' => false, 'error' => "Left: {$e->getMessage()}"];
}
try {
$right = json_decode($right_raw, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return ['changes' => [], 'same' => false, 'error' => "Right: {$e->getMessage()}"];
}
$changes = [];
json_diff_walk($left, $right, '$', $changes);
return ['changes' => $changes, 'same' => count($changes) === 0, 'error' => null];
}
// Example:
// json_diff('{"name": "ada"}', '{"name": "grace"}')['changes']
// → [['path' => '$.name', 'type' => 'changed', 'before' => 'ada', 'after' => 'grace']]
// json_diff('[1, 2]', '[1, 2, 3]')['changes']
// → [['path' => '$[2]', 'type' => 'added', 'after' => 3]]
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →