toolqik

130 · JSON

JSON Merge

Deep-merge two or more JSON documents, or apply a JSON Merge Patch (RFC 7396).

Runs in your browser
Mode
Arrays
Indent
Merged JSON

  

    About this tool

    Merging JSON is how layered configuration works: a base file with defaults, then environment-specific files that override some values. This tool deep-merges two or more JSON documents in order, so later documents win, and lists every value that was overridden so you can check the result.

    Deep merge vs. shallow merge

    A shallow merge (like JavaScript’s {...a, ...b} or Object.assign) replaces whole nested objects: merging {"db": {"host": "a", "port": 5432}} with {"db": {"host": "b"}} loses port. A deep merge combines nested objects key by key, giving {"db": {"host": "b", "port": 5432}}. That’s what this tool does.

    How arrays are merged

    • Replace (default): the later array replaces the earlier one. This matches most config systems and lodash merge with arrays treated as values.
    • Concatenate: items from all documents, in order.
    • Union: like concatenate, but identical items appear once.
    • Merge by position: item 0 merges with item 0 and so on (what lodash _.merge does).

    JSON Merge Patch (RFC 7396)

    Merge Patch is a standard way to describe changes to a JSON document, used by HTTP PATCH requests with the application/merge-patch+json content type and by tools such as kubectl patch --type merge. It works like a deep merge with one extra rule: a null value deletes the key. Arrays are always replaced. Choose Merge Patch mode and put the original document first and the patch second.

    Example

    Base:     { "app": { "port": 8080, "debug": false }, "cors": { "origins": ["*"] } }
    Override: { "app": { "debug": true }, "cors": null }
    
    Deep merge:  { "app": { "port": 8080, "debug": true }, "cors": null }
    Merge Patch: { "app": { "port": 8080, "debug": true } }

    To see the differences between two documents rather than combine them, use the JSON diff checker.

    FAQ

    How do I merge two JSON objects?

    Paste the base JSON as document A and the overriding JSON as document B. Nested objects are merged key by key and values from B win. Add more documents to merge several in order.

    What is the difference between deep merge and Object.assign?

    Object.assign and the spread operator are shallow: a nested object in the second document replaces the whole nested object from the first. A deep merge combines nested objects key by key.

    How are arrays merged?

    By default the later array replaces the earlier one. You can concatenate them, take the unique union, or merge items by position.

    What is JSON Merge Patch?

    RFC 7396 defines a patch format where you send only the fields to change. Objects merge recursively, null deletes a key, and everything else replaces. It is used with HTTP PATCH and kubectl.

    How do I delete a key when merging?

    Use Merge Patch mode and set the key to null in the later document. In deep merge mode, null is kept as a value.

    esc
    ↑↓ navigate↵ open