aboutsummaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
Diffstat (limited to 'misc')
-rwxr-xr-xmisc/bin/merge-json.sh16
1 files changed, 16 insertions, 0 deletions
diff --git a/misc/bin/merge-json.sh b/misc/bin/merge-json.sh
new file mode 100755
index 0000000..871176a
--- /dev/null
+++ b/misc/bin/merge-json.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+# Merge two json objects
+# 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({}; .)' "$1" "$2"