1

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

Rajiv Rai
  • 235
  • 4
  • 16
  • 1
    single quote the variable content? – sjsam Apr 29 '19 at 10:48
  • above variable is what i am getting as a sql query output, does not sound feasible to me to make single quote these items. – Rajiv Rai Apr 29 '19 at 10:51
  • 2
    If you're storing the sql results in a variable, use process substitution to directly feed the results into jq like `< <(sqlite first query ; sqlite second query)` – sjsam Apr 29 '19 at 10:54
  • sjsam, single quote also does not seem to work, used this. ```sed "s/\"/'/g" <<< "$local" > local``` ```sed "s/\"/'/g" <<< "$org" > org``` – Rajiv Rai Apr 29 '19 at 11:06
  • 2
    If you have initialised those variables like `local=[{"account_id":...` all those double quotes are gone. You need to enclose json value in single quotes wile assigning them to variables, e.g `local='[{"account_id":...'` – oguz ismail Apr 29 '19 at 11:21
  • oguz, realised my mistake. quoting the json in a single quote fixed the issue. – Rajiv Rai Apr 29 '19 at 11:34
  • Possible duplicate of [Concatenate two JSON objects](https://stackoverflow.com/q/433627/608639), [Merge two json/javascript arrays in to one array](https://stackoverflow.com/q/10384845/608639), etc. – jww Apr 29 '19 at 16:26

2 Answers2

1

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]"
choroba
  • 231,213
  • 25
  • 204
  • 289
0

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" } ]
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • your bracket is misplaced in ```echo``` statement? i used this, ``` echo $(jq -s '.[0] + .[1]') < – Rajiv Rai Apr 29 '19 at 11:20
  • @RajivRai Nothing is misplaced, nor does it hang. Just copy and paste the entire script into a file and run it to see. – Shawn Apr 29 '19 at 11:22
  • executed exactly what you have given here and it is throwing error. ./test.bash: line 20: warning: here-document at line 20 delimited by end-of-file (wanted `EOF') ./test.bash: line 19: warning: here-document at line 19 delimited by end-of-file (wanted `EOF') null ./test.bash: line 20: [{"account_id":"01C","id":"0XVWKCFV6P9CA5"},{"account_id":"CSDTHQ","id":"631QGYBNSF"}]: command not found ./test.bash: line 21: [{"account_id":"BJPKWSH","id":"15BS0XP4F91V6YH4G0PV"},{"account_id":"01BKK44V5F6A1FKH60Q0ANX9JX","id":"01BKK44V7"}]: command not found ./test.bash: line 22: EOF: command not found – Rajiv Rai Apr 29 '19 at 11:24
  • @RajivRai Please stop trying to put chunks of code or error messages or anything else split up by lines in a comment. It doesn't work. – Shawn Apr 29 '19 at 11:25
  • @RajivRai If you're getting an error on line 22, you didn't copy and paste the script in my answer because it doesn't have that many lines. – Shawn Apr 29 '19 at 11:25
  • @RajivRai Fixed to work with bash. (I was testing with dash, which accepts the original version, though with more testing bash, zsh and ksh93 didn't. My bad.) – Shawn Apr 29 '19 at 11:28
  • Thanks Shawn, even what i was trying earlier is working now but after quoting the json into single course(' ') just like you did. I think i was initialising the variable incorrectly. Your solution is also working. Thanks. – Rajiv Rai Apr 29 '19 at 11:30