Skip to content
UniCircle
MediumCodingObjects

Deep equality of two JSON values

Question JS-COD-004

Write deepEqual(a, b) for JSON values and print whether the two inputs are deeply equal.

Two values are deeply equal when:

  • both are the same primitive (number, string, boolean or null) with the same value and type — no coercion, so 1 and "1" differ, and true and 1 differ;
  • both are arrays of the same length whose elements are deeply equal position by position;
  • both are objects (not arrays) with exactly the same set of keys, and deeply equal values for each key. Key order does not matter.

Constraints

  • Each line is valid JSON of at most 5000 characters.
  • Numbers are integers. Objects have no duplicate keys.

Input and output

Input
Line 1: the first JSON value Line 2: the second JSON value
Output
`true` or `false`

Examples

Input 1

{"a":1,"b":[1,2,{"c":true}]}
{"b":[1,2,{"c":true}],"a":1}

Output

true

Same keys and values; object key order does not matter.

Input 2

[1,2,3]
[1,3,2]

Output

false

Array order matters.

Input 3

{"n":1}
{"n":"1"}

Output

false

The number 1 and the string "1" are different (no coercion).

Show hints
  • Handle null before checking typeof "object".
  • An array and an object can have the same keys (0, 1, …), so test Array.isArray on both sides.
Show the explanation

Recursive comparison:

  1. If a === b, they are equal (covers equal primitives).
  2. If either is not an object, or either is null, they are not equal (different primitives, or primitive vs object).
  3. If one is an array and the other is not, not equal.
  4. Arrays: same length and every element pair deeply equal.
  5. Objects: the same number of keys, and every key of a exists as an own key in b with a deeply equal value.

Checking own keys matters: { a: undefined } is not the same as {} in JavaScript, and {"a": null} is not the same as {} in JSON. Libraries such as Lodash's isEqual also handle dates, maps, sets and cycles, which plain JSON does not have.

Original UniCircle question. Last reviewed 22 Oct 2026.