Blogchevron_rightJSON Tools
JSON Tools

JSON Merge: How to Combine Two JSON Objects Safely

Merging JSON objects is a frequent operation in API development and configuration management. Here is how to do it correctly and safely.

April 18, 2026·7 min read

Shallow Merge in JavaScript

Object.assign(target, source) and the spread operator { ...obj1, ...obj2 } both perform shallow merges. Duplicate keys from source override those in target. This works correctly for flat objects but produces unexpected results with nested objects — the nested object from source completely replaces the one in target rather than merging with it.

const merged = { ...defaults, ...overrides } is the most common merge pattern. The right-side object wins all key conflicts. This is appropriate when overrides should completely replace any default value, including nested objects.

Deep Merge

Deep merge recursively merges nested objects rather than replacing them. If both objects have a settings key that is an object, deep merge combines the settings from both rather than replacing defaults.settings with overrides.settings entirely.

Lodash provides _.merge(object, sources): const result = _.merge({}, defaults, overrides). It merges recursively, with right-side values winning for scalar conflicts. Be aware that _.merge mutates the first argument — always pass {} as the first argument to avoid modifying your defaults.

Merge Strategies for API Responses

When combining data from multiple API endpoints, define an explicit merge strategy for each field. For arrays, decide whether to concatenate (all items from both), union (deduplicated), or replace (take one source's array entirely). Arrays have no universal correct merge strategy — the right approach depends on the semantics of the data.

For configuration merging (default config + user overrides + environment overrides), deep merge is usually correct: environment overrides win for specific keys but default values fill in for everything not explicitly overridden.

Using JSON Compare to Validate a Merge

After merging two JSON objects, compare the result to both sources using a JSON diff tool. Verify that all expected fields from both sources appear in the merged result with the correct values. This validation step catches cases where the merge algorithm produced unexpected results — particularly for nested objects and arrays.

If the merge result differs from your expectation, the diff shows exactly which fields are wrong. This is faster than debugging the merge code and helps you understand whether the issue is with the merge strategy (shallow vs deep) or the conflict resolution (which source wins).

Try JSON Compare Free Online

No sign-up required. 100% client-side — your data never leaves your browser.

Open JSON Comparearrow_forward

Frequently Asked Questions

How do I merge JSON objects in Python?

Shallow merge: merged = {**dict1, **dict2}. For a recursive deep merge, use a custom function or the deepmerge library (pip install deepmerge): from deepmerge import always_merger; result = always_merger.merge({}, dict1, dict2).

What happens to duplicate array elements when merging JSON?

It depends on the merge strategy. Lodash _.merge replaces array values by index. Object spread replaces entire arrays. For union merging (deduplicated combination), write a custom function or use a library that supports it.

Can I merge JSON objects with different schemas?

Yes, but validate the result. Merging objects with different schemas can produce unexpected types or missing required fields. Validate the merged result against a JSON schema to ensure it conforms to the expected structure.

JSON Merge: How to Combine Two JSON Objects Safely