Merge JSON documents
How JSON merging works, where shallow merge, deep merge and JSON Merge Patch differ, and when a merged result stays valid, readable and safe to share.
Merging JSON sounds like one operation, but tools use the word for several different transformations. Some combine two objects key by key. Some apply a patch with deletion rules. Some simply join files end to end. Before you trust a merged result, you need to know which of these the tool actually performs and whether the output still means what you need it to mean.
Evidence note: no specific merge tool was tested for this review, and no product measurements, prices or benchmarks were available. The guidance below relies on the JSON specification, the JSON Merge Patch standard and widely documented language behavior. Where behavior depends on a particular library or service, check that tool's documentation.
Is merging JSON suitable for your use?
Merging works well when you have a base document and a smaller set of overrides. Typical cases are layered configuration (defaults plus environment-specific values), applying partial updates to a stored record, and combining settings from several sources into one file. The main benefit is that you only write or send what changes instead of maintaining full copies.
The decisive tradeoff is that every merge strategy makes choices on your behalf: what happens to arrays, what a null value means, and which side wins a conflict. If those choices do not match your intent, the output is still valid JSON but quietly carries the wrong data. Nothing in the result signals that a list was replaced instead of extended, or that a key was deleted instead of set to null.
Keeping documents separate, or editing one plain document by hand, is often the better choice when:
- The data relies heavily on arrays whose items must be combined, matched by ID or deduplicated.
- Null is a meaningful value that must be preserved rather than treated as a delete instruction.
- The document is signed, hashed or compared byte for byte, because re-serialization can change bytes without changing values.
- You need an audit trail of exactly what changed, which an explicit list of operations records better than a merged snapshot.
- Only one or two values differ and a direct edit is easier to review than a merge rule.
What a JSON merge actually changes
Unlike pretty-printing, which only changes whitespace and leaves the data untouched, a merge changes the stored data. It produces a new set of keys and values. Four meanings of "merge" are common, and they produce different results from the same input.
| Approach | Nested objects | Arrays | Null in the override |
| Shallow merge (for example JavaScript object spread or Python dict unpacking) | Replaced entirely by the override's object | Replaced | Stored as null |
| Recursive or deep merge (for example the jq * operator) | Merged key by key | Usually replaced; some libraries concatenate | Usually stored as null; varies by library |
| JSON Merge Patch (RFC 7396) | Merged key by key | Replaced wholesale | Removes the key |
| File concatenation | Not merged | Not merged | Not interpreted |
File concatenation needs its own warning. Placing two JSON objects one after the other does not produce a single valid JSON document. It only makes sense for formats designed for it, such as newline-delimited JSON (JSON Lines), where each line is a separate record. If a tool describes "merging" files but the output has two top-level values, it has concatenated them.
Here is one base document and one override. They are used below to show how the strategies diverge.
Base:
{
"name": "service-a",
"port": 8080,
"features": ["auth", "logging"],
"db": { "host": "localhost", "pool": 5 }
}
Override:
{
"port": 9090,
"features": ["metrics"],
"db": { "pool": 10, "host": null },
"region": "us-east"
}
Applied as a JSON Merge Patch, the null deletes db.host, the features array is replaced, db.pool is updated, and the new region key is added:
{
"name": "service-a",
"port": 9090,
"features": ["metrics"],
"db": { "pool": 10 },
"region": "us-east"
}
A shallow merge replaces the whole db object with the override's version, so the null is stored literally:
{
"name": "service-a",
"port": 9090,
"features": ["metrics"],
"db": { "pool": 10, "host": null },
"region": "us-east"
}
A recursive merge with jq produces the same output here, because jq merges nested objects but lets the right-hand value win for every non-object value, null included. The difference from a shallow merge would show if the override's db object omitted host entirely: jq would keep "localhost", and a shallow merge would drop it.
jq -s '.[0] * .[1]' base.json override.json
A deep-merge library that concatenates arrays would instead return features as ["auth", "logging", "metrics"]. Neither behavior is wrong. They answer different questions, so confirm which one your tool uses before you rely on it.
Merge versus patch
JSON Patch (RFC 6902) is a separate standard that is often confused with JSON Merge Patch. Instead of an override document, it is a list of explicit operations such as add, remove, replace, move and test, with paths to the values they affect. It can target individual array elements and can express "set this to null", which Merge Patch cannot. The cost is that it is more verbose and harder to read at a glance. If you need array-level edits or need null to survive, JSON Patch fits better than a merge.
Details that silently change meaning
- Duplicate keys: the JSON specification says object names should be unique and warns that behavior with duplicates is unpredictable. Many parsers keep the last value, but some keep the first or raise an error. If an input contains duplicates, the merged result may not reflect the value you expected.
- Key order: JSON objects are unordered by definition. A merge tool may reorder keys. That does not change the data, but it can make diffs noisy and confuse readers who expect a fixed order.
- Large numbers: many implementations store numbers as double-precision floats, so integers beyond 2^53 or long decimals can lose precision when the document is parsed and written back. This matters for IDs and monetary values.
- Type conflicts: if one side has an object and the other has a string or array at the same key, most strategies let the override replace the value outright, with no warning.
Will the merged result stay readable and usable after copying or sharing?
A merged JSON document is ordinary text, so it copies and pastes like any other text. The risks come from re-serialization and from the places you paste it, not from the merge logic.
- Re-serialization: a tool may change whitespace, indentation, key order or escaping, for example writing \u00e9 as é or the reverse. The values are equal, but the bytes differ. That breaks signatures and checksums and can produce large diffs in version control.
- Comments and trailing commas: standard JSON allows neither. If your source files use a relaxed variant, such as JSONC in some editors, a strict merge tool may reject them or strip the comments without notice.
- Encoding: the JSON specification requires UTF-8 for documents exchanged between systems. Output saved in another encoding, or with a byte-order mark, may fail in strict parsers.
- Rich-text destinations: pasting into word processors, some chat apps or email clients can turn straight quotes into curly ones or insert non-breaking spaces, and the result no longer parses. Use code blocks or plain-text fields when sharing JSON through these channels.
Readability depends mostly on formatting. A minified merged document is compact but hard to review. Pretty-printing it afterward changes only whitespace and makes it much easier to inspect, which is worth doing before you commit or share a merged configuration.
Accessibility: no documentation or scoped tests were available on how specific screen readers handle large or deeply nested JSON, so no claim is made about particular assistive technologies. As a general principle, consistent indentation, modest nesting depth and short key names help anyone reading the file in a plain-text or code view. When the audience is not technical, a short plain-language summary of what changed is more usable than the raw merged document.
Searching and editing: once documents are merged, the result no longer shows which file a value came from. If people will later need to edit the defaults or the overrides separately, keep the source files as the source of truth and regenerate the merged output, rather than editing the merged copy by hand.
Online merge tools and sensitive data
Many merge utilities run in a browser or on a remote server. You cannot tell from a tool's appearance or marketing whether it processes data locally or keeps it. Configuration files often hold credentials, tokens or internal hostnames. For those, use a local command-line tool or a library in your own code, or remove secrets before pasting anything into a web service.
Checks before trusting a merged result
- Confirm which strategy the tool uses: shallow, recursive or Merge Patch, plus its rules for arrays and null.
- Run a small test like the example above, with a nested object, an array and a null, and compare the output to what you expected.
- Validate the output with a strict JSON parser, and against a schema if you have one.
- Check that large integers and precise decimals survived unchanged.
- Diff the merged result against the base to see every changed, added and removed key.
- Keep the original inputs so you can reproduce or audit the merge.
Bottom line: merging is a sound way to layer overrides onto a base document, provided the tool's handling of arrays, null and conflicts matches your intent. When those rules do not fit, or when exact bytes and change history matter, an explicit JSON Patch or a hand-edited plain document is the safer option.