Merge JSON objects with jq

How jq's + and * operators merge JSON objects, what each one changes, how arrays, nulls and key order behave, and when a different approach is safer.

Merging JSON objects with jq is a common task when combining configuration files, layering defaults with overrides, or folding API responses together. jq handles it with two operators that behave differently, and choosing the wrong one can silently drop data. This review explains what each approach changes, where the results can surprise you, and how to check a merge before you rely on it.

Is jq suitable for your merge?

For most override-style merges, jq is a good fit. It is scriptable, it works on files or piped input, and its merge rules are simple and predictable once you know them. The main tradeoff is that both built-in merge operators let the right-hand value win on conflict and never combine arrays. If you need arrays concatenated, conflicts reported instead of resolved, or a reviewable record of what changed, you will need extra jq code or a manual edit.

  • jq fits well when: you are applying overrides on top of defaults; later values should replace earlier ones; the merge runs in a script or CI step that must be repeatable.
  • A manual edit or a plain text review is preferable when: the merge is a one-off with only a few keys; conflicts need human judgment rather than last-one-wins; the files contain comments or trailing commas, which are not valid JSON and which jq will reject; someone must approve every changed value.

What a jq merge actually changes

A merge changes the data itself. Keys are added, and values are replaced. That is different from formatting choices such as indentation, compact output or sorted keys, which change only how the same data is displayed. The word "merge" is also used loosely for several different operations: a shallow merge (top-level keys only), a deep or recursive merge (nested objects combined), array concatenation, and combining many files into one. jq covers each of these differently.

Shallow merge with +

Adding two objects with + combines their top-level keys. When both objects have the same key, the value from the right-hand object replaces the left-hand value entirely, even if both values are nested objects.

$ jq -n '{"a":{"b":1,"c":2}} + {"a":{"b":3},"d":4}'
{
  "a": {
    "b": 3
  },
  "d": 4
}

Note that "c" is gone. The whole nested object under "a" was replaced, not merged.

Recursive merge with *

Multiplying two objects with * merges recursively. When both sides have an object at the same key, those objects are merged too. For any other combination of types, the right-hand value wins.

$ jq -n '{"a":{"b":1,"c":2}} * {"a":{"b":3},"d":4}'
{
  "a": {
    "b": 3,
    "c": 2
  },
  "d": 4
}
Behavior | + (shallow) | * (recursive)
Top-level key conflict | Right value wins | Right value wins unless both are objects
Nested objects at same key | Right object replaces left | Merged key by key
Arrays at same key | Right array replaces left | Right array replaces left
Keys only on one side | Kept | Kept

Merging files and many objects

To merge separate files, read them together. The --slurp option (-s) collects all inputs into one array. The add filter then applies + across that array, giving a shallow merge. For a recursive merge across many inputs, use reduce with *. Inputs are processed in order, so files listed later override earlier ones.

# Shallow merge of two files
jq -s '.[0] + .[1]' defaults.json overrides.json

# Shallow merge of any number of files
jq -s 'add' a.json b.json c.json

# Recursive merge of any number of files
jq -s 'reduce .[] as $o ({}; . * $o)' a.json b.json c.json

One edge case: add on an empty array returns null, not an empty object. If your file list can be empty, the reduce form starting from {} is the safer choice.

Arrays are replaced, not combined

Neither + nor * concatenates arrays inside objects. If you merge a list of plugins or hosts and expect both lists to survive, only the right-hand list will remain. To concatenate arrays during a deep merge, define your own function. The version below merges objects recursively, joins arrays, and otherwise lets the right-hand value win. It does not remove duplicates from the joined arrays.

jq -s '
  def deepmerge($a; $b):
    if ($a|type) == "object" and ($b|type) == "object" then
      reduce ($b|keys_unsorted[]) as $k ($a; .[$k] = deepmerge($a[$k]; $b[$k]))
    elif ($a|type) == "array" and ($b|type) == "array" then
      $a + $b
    else
      $b
    end;
  deepmerge(.[0]; .[1])
' a.json b.json

Changes that are easy to miss

  • Explicit null overrides: {"a":1} + {"a":null} produces {"a":null}. A null in the override file is treated as a real value, not as "leave unchanged".
  • Key order: jq keeps keys in insertion order by default. Keys that already existed on the left keep their position, and new keys are appended. Use -S if you want sorted keys for stable diffs; this changes presentation only, not the data.
  • Duplicate keys in one input: if a single file repeats a key, jq keeps the last occurrence, so part of that file can disappear before the merge even starts.
  • Large numbers: older jq releases convert numbers to double-precision floating point, which can alter very large integers such as some IDs. The jq 1.7 release notes describe preserving the original precision of number literals that are not modified. Check your installed version with jq --version if your data contains large integers.

Will the result stay usable after copying or sharing?

The output of a jq merge is ordinary JSON text, so it copies, pastes, diffs and edits like any other text file. It does not use special characters or visual tricks that could break search or editing. Readability depends mostly on formatting. The default pretty-printed output is easier to scan and review, while -c produces a single compact line that is convenient for logs and pipelines but hard to read when deeply nested. For long nested output on a single line, people using screen readers or magnification may also find structure harder to follow, so share pretty-printed output when humans need to review it. This review did not test any specific assistive technology or destination application.

The commands themselves are less portable than the output. The single-quoted filters above assume a Unix-style shell such as bash or zsh. Windows Command Prompt handles quotes differently, so a filter copied from a tutorial may fail there. Putting the filter in a file and running it with jq -f avoids most quoting problems.

Checking a merge before you rely on it

  • Confirm each input is valid JSON first: jq empty file.json exits with an error if the file cannot be parsed.
  • Decide in advance whether you need shallow, recursive or array-concatenating behavior, and pick + , * or a custom function to match.
  • Search override files for explicit null values and confirm they are intended.
  • Compare the result against the expected output with sorted keys, for example: diff <(jq -S . expected.json) <(jq -S . merged.json)
  • If the data includes large integers, spot-check them in the merged output.

Limits of this review

No version-specific test results, benchmarks or environment tests were supplied for this article. The behavior described is based on jq's documented operators and general shell behavior. Exact output formatting, number handling and function syntax support can vary between jq releases, so verify against the version you actually run before using a merge in production.