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

7
  • 1
    single quote the variable content? Commented Apr 29, 2019 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. Commented Apr 29, 2019 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) Commented Apr 29, 2019 at 10:54
  • sjsam, single quote also does not seem to work, used this. sed "s/\"/'/g" <<< "$local" > local sed "s/\"/'/g" <<< "$org" > org Commented Apr 29, 2019 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":...' Commented Apr 29, 2019 at 11:21

2 Answers 2

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]"
Sign up to request clarification or add additional context in comments.

Comments

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" } ]

7 Comments

your bracket is misplaced in echo statement? i used this, ``` echo $(jq -s '.[0] + .[1]') <<EOF ``` $j1 $j2 EOF and it's stuck waiting for the input.
@RajivRai Nothing is misplaced, nor does it hang. Just copy and paste the entire script into a file and run it to see.
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
@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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.