Skip to content

Instantly share code, notes, and snippets.

@rgpower
Created October 19, 2022 11:46
Show Gist options
  • Save rgpower/f8fd8488f445686d934c825ca7e65c54 to your computer and use it in GitHub Desktop.
Save rgpower/f8fd8488f445686d934c825ca7e65c54 to your computer and use it in GitHub Desktop.
use jq to merge json files
# courtesy of Szymon Stepniak
# See https://e.printstacktrace.blog/merging-json-files-recursively-in-the-command-line/
jq -s 'def deepmerge(a;b):
reduce b[] as $item (a;
reduce ($item | keys_unsorted[]) as $key (.;
$item[$key] as $val | ($val | type) as $type | .[$key] = if ($type == "object") then
deepmerge({}; [if .[$key] == null then {} else .[$key] end, $val])
elif ($type == "array") then
(.[$key] + $val | unique)
else
$val
end)
);
deepmerge({}; .)' *.json > merged.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment