how to combine two json variables using jq

Viewed 514

Is there a simple way to combine two json variables? Given $a {a:1,b:1} and $b {c:1,d:1} How do I produce a result of {a:1,b:1,c:1,d:1}

2 Answers

Yes, e.g.

$a + $b

or

 [$a,$b]|add

For example, if $a and $b are initially JSON-valued shell variables:

jq -n --argjson a "$a" --argjson b "$b" '$a + $b'

Try the following:

a='{"a":1,"b":1}'
b='{"c":1,"d":1}'

jq --slurp 'add' <(echo "$a") <(echo "$b")

Output:

{
  "a": 1,
  "b": 1,
  "c": 1,
  "d": 1
}
Related