Created
October 19, 2022 11:46
-
-
Save rgpower/f8fd8488f445686d934c825ca7e65c54 to your computer and use it in GitHub Desktop.
use jq to merge json files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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