我有两个json数组,我已经初始化如下。
local=[{"account_id":"01C","id":"0XVWKCFV6P9CA5"},{"account_id":"CSDTHQ","id":"631QGYBNSF"},...............]
org=[{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"},.....................]
我想要的是下面这样的东西。
outputJson=[{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},
{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},
{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"},.....................]
我想把这两个json数组合并成一个。
我试过了,
jq -s '.[0] * .[1]' <<< "$local $org"
但它给了
parse error: Invalid literal at line 1, column 17
最佳答案
使用+
,而不是*
:
#!/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
)
生产:
[ { "account_id": "01C", "id": "0XVWKCFV6P9CA5" }, { "account_id": "CSDTHQ", "id": "631QGYBNSF" }, { "account_id": "BJPKWSH", "id": "15BS0XP4F91V6YH4G0PV" }, { "account_id": "01BKK44V5F6A1FKH60Q0ANX9JX", "id": "01BKK44V7" } ]