Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame^] | 1 | # https://stackoverflow.com/a/53666584/2245623 |
| 2 | # Recursively meld a and b, concatenating arrays and favoring b when there is a conflict |
| 3 | def meld(a; b): |
| 4 | a as $a | b as $b |
| 5 | | if ($a|type) == "object" and ($b|type) == "object" |
| 6 | then reduce ([$a,$b]|add|keys_unsorted[]) as $k ({}; |
| 7 | .[$k] = meld( $a[$k]; $b[$k]) ) |
| 8 | elif ($a|type) == "array" and ($b|type) == "array" |
| 9 | then $a+$b |
| 10 | elif $b == null then $a |
| 11 | else $b |
| 12 | end; |
| 13 | |
| 14 | reduce inputs as $i (.; meld(.; $i)) |