Deep Clone a JSON Object
Difficulty: Medium · Topics: JSON, Recursion, Deep Copy
Difficulty: Medium | Tags: JSON, Recursion, Deep Copy
Description
Given a JSON object (nested maps, lists, and primitives), create a deep clone — a completely independent copy where no nested references are shared with the original.
Implement:
deepClone(jsonObject) → Object
Examples
Example 1:
Input: {"a": 1, "b": {"c": [1, 2, 3]}}
Output: {"a": 1, "b": {"c": [1, 2, 3]}}
(and modifying the clone does NOT affect the original)
Example 2:
Input: [{"x": 10}, {"y": 20}]
Output: [{"x": 10}, {"y": 20}]
Constraints
- Nesting depth ≤ 20.
- Values: String, Number, Boolean, null, Map, List.
- No circular references.
Hints
Hint 1
Recursively copy: if Map → new map with cloned values; if List → new list with cloned elements; else return value directly (primitives are immutable).