Rajiv Rai
Rajiv Rai

Reputation: 245

Merging two json arrays (loaded into memory) into one json array

I have two json array, which i have initialised as given below.

local=[{"account_id":"01C","id":"0XVWKCFV6P9CA5"},{"account_id":"CSDTHQ","id":"631QGYBNSF"},...............]

org=[{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"},.....................]

what i want is something like below.

outputJson=[{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},
{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},
{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"},.....................]        

i.e. i want to merge these two json arrays into one. I tried this,

jq -s '.[0] * .[1]' <<< "$local $org"

but it is giving parse error: Invalid literal at line 1, column 17

Upvotes: 1

Views: 1237

Answers (2)

Shawn
Shawn

Reputation: 52644

Use +, not *:

#!/bin/sh

j1='[{"account_id":"01C","id":"0XVWKCFV6P9CA5"},{"account_id":"CSDTHQ","id":"631QGYBNSF"}]'
j2='[{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"}]'
echo $(jq -s '.[0] + .[1]' <<EOF
$j1
$j2
EOF
)

produces:

[ { "account_id": "01C", "id": "0XVWKCFV6P9CA5" }, { "account_id": "CSDTHQ", "id": "631QGYBNSF" }, { "account_id": "BJPKWSH", "id": "15BS0XP4F91V6YH4G0PV" }, { "account_id": "01BKK44V5F6A1FKH60Q0ANX9JX", "id": "01BKK44V7" } ]

Upvotes: 0

choroba
choroba

Reputation: 242383

I'm not sure I understand what you need. If you need to just merge the two arrays into one, you can use

jq '[.[0][], .[1][]]' <<< "[$local, $org]"

Upvotes: 1

Related Questions