s-a-n
s-a-n

Reputation: 787

how to combine two json variables using jq

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}

Upvotes: 2

Views: 3544

Answers (2)

peak
peak

Reputation: 117027

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'

Upvotes: 5

j1-lee
j1-lee

Reputation: 13939

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
}

Upvotes: 2

Related Questions