aboutsummaryrefslogtreecommitdiff
path: root/misc/bin/merge-json.sh
blob: 871176a5f06376246c7b6bec0702f682a2390cc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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"