How to Deep Merge JSON Objects
JSON Merging
When to Merge JSON
Combine default and override configs, merge API partial updates, or join environment-specific settings into one object. Paste two JSON objects — the second overrides the first at conflicting keys.
Real-World Examples
Base config + environment overrides → single deployable config
{"a":1,"b":{"x":1}} + {"b":{"y":2}} → {"a":1,"b":{"x":1,"y":2}}
Combine user defaults with saved preferences in a settings object
Common Mistakes
- Expecting arrays to concatenate — arrays from the second object replace the first entirely
- Shallow merge expectations — nested objects are merged recursively, not replaced wholesale
- Merging invalid JSON — validate both inputs first
Developer Tips
- Put defaults in the first object, overrides in the second
- Sort keys after merging for stable config diffs in git
- For precise diffs, use JSON Patch instead of full merge
Frequently asked questions
How are nested objects merged?
Nested objects are merged recursively. Keys from the second object override or extend keys in the first.
What happens to arrays?
Arrays are replaced, not merged element by element. The second object array wins at that key.
Which object takes priority?
The second JSON object wins on conflicting keys. Non-conflicting keys from both are preserved.