Skip to content
RED SAGSkills
medium30 ptsfrontendrecursion

Deep Diff of Two JSON Objects

An audit log records what changed between two versions of a settings object. Given plain objects before and after, return the list of changes.

Walk both objects together. For a key only in before, emit { path, type: "removed", value }. For a key only in after, emit { path, type: "added", value }. If both values are plain objects (not arrays, not null), recurse into them. Otherwise, if the values are not deeply equal, emit { path, type: "changed", from, to }. Arrays are compared as whole values (deep equality), never recursed into.

path is the keys joined with .. Return the changes sorted by path ascending in plain string order.

Example 1

Input: before = {"name":"A","age":30,"address":{"city":"Dubai","zip":"1"},"tags":["x"]}, after = {"name":"A","age":31,"address":{"city":"Dubai"},"tags":["x","y"],"email":"a@x.io"}
Output: [{"path":"address.zip","type":"removed","value":"1"},{"path":"age","type":"changed","from":30,"to":31},{"path":"email","type":"added","value":"a@x.io"},{"path":"tags","type":"changed","from":["x"],"to":["x","y"]}]
Explanation: age changed, zip removed, email added, tags (an array) changed.

Example 2

Input: before = {"a":1}, after = {"a":1}
Output: []

Constraints

  • Objects hold only JSON values
  • Nesting depth <= 20
  • Keys do not contain .
Hint 1

Collect the union of keys at each level.

Hint 2

Write your own deepEqual; JSON.stringify breaks when key order differs.

Good practice for: Frontend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Backend Developer, DevOps Engineer

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.