Convert a JSON string to an array in Bash [closed]

Multi tool use
Am having a curl function to fetch data from webapi. the web API returns a Json String of format ["Name1","Name2","Name3","Name4","Name5"]
. I need to extract the data into an array in the bash script so that I could use the Names to loop in the rest of the code.
My required format is like arr[0]=Name1 ,...., arr[n]=Namen
linux bash asp.net-web-api
closed as too broad by jww, Stephen Kennedy, eyllanesc, Paul Roub, Jean-François Fabre Dec 31 '18 at 22:29
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Am having a curl function to fetch data from webapi. the web API returns a Json String of format ["Name1","Name2","Name3","Name4","Name5"]
. I need to extract the data into an array in the bash script so that I could use the Names to loop in the rest of the code.
My required format is like arr[0]=Name1 ,...., arr[n]=Namen
linux bash asp.net-web-api
closed as too broad by jww, Stephen Kennedy, eyllanesc, Paul Roub, Jean-François Fabre Dec 31 '18 at 22:29
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52
add a comment |
Am having a curl function to fetch data from webapi. the web API returns a Json String of format ["Name1","Name2","Name3","Name4","Name5"]
. I need to extract the data into an array in the bash script so that I could use the Names to loop in the rest of the code.
My required format is like arr[0]=Name1 ,...., arr[n]=Namen
linux bash asp.net-web-api
Am having a curl function to fetch data from webapi. the web API returns a Json String of format ["Name1","Name2","Name3","Name4","Name5"]
. I need to extract the data into an array in the bash script so that I could use the Names to loop in the rest of the code.
My required format is like arr[0]=Name1 ,...., arr[n]=Namen
linux bash asp.net-web-api
linux bash asp.net-web-api
edited Dec 31 '18 at 12:24


Cyrus
45.9k43878
45.9k43878
asked Dec 31 '18 at 12:05
user3474213user3474213
63
63
closed as too broad by jww, Stephen Kennedy, eyllanesc, Paul Roub, Jean-François Fabre Dec 31 '18 at 22:29
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by jww, Stephen Kennedy, eyllanesc, Paul Roub, Jean-François Fabre Dec 31 '18 at 22:29
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52
add a comment |
1
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52
1
1
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52
add a comment |
1 Answer
1
active
oldest
votes
For elements without newlines you could do something as simple as this:
$ readarray -t arr < <(jq -r '.' <<< '["Name1","Name2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]="Name2")
Reading this jq
request for NUL-delimited output, this will allow elements with newlines as well:
while IFS= read -rd '' item; do
arr+=("$item")
done < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
# with bash 4.4 or later
$ readarray -d '' arr < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]=$'Nanme2')
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For elements without newlines you could do something as simple as this:
$ readarray -t arr < <(jq -r '.' <<< '["Name1","Name2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]="Name2")
Reading this jq
request for NUL-delimited output, this will allow elements with newlines as well:
while IFS= read -rd '' item; do
arr+=("$item")
done < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
# with bash 4.4 or later
$ readarray -d '' arr < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]=$'Nanme2')
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
add a comment |
For elements without newlines you could do something as simple as this:
$ readarray -t arr < <(jq -r '.' <<< '["Name1","Name2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]="Name2")
Reading this jq
request for NUL-delimited output, this will allow elements with newlines as well:
while IFS= read -rd '' item; do
arr+=("$item")
done < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
# with bash 4.4 or later
$ readarray -d '' arr < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]=$'Nanme2')
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
add a comment |
For elements without newlines you could do something as simple as this:
$ readarray -t arr < <(jq -r '.' <<< '["Name1","Name2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]="Name2")
Reading this jq
request for NUL-delimited output, this will allow elements with newlines as well:
while IFS= read -rd '' item; do
arr+=("$item")
done < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
# with bash 4.4 or later
$ readarray -d '' arr < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]=$'Nanme2')
For elements without newlines you could do something as simple as this:
$ readarray -t arr < <(jq -r '.' <<< '["Name1","Name2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]="Name2")
Reading this jq
request for NUL-delimited output, this will allow elements with newlines as well:
while IFS= read -rd '' item; do
arr+=("$item")
done < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
# with bash 4.4 or later
$ readarray -d '' arr < <(jq -j '.|(. + "u0000")' <<< '["Name1","Nanme2"]')
$ declare -p arr
declare -a arr=([0]="Name1" [1]=$'Nanme2')
edited Dec 31 '18 at 14:45
answered Dec 31 '18 at 12:29
mickpmickp
687110
687110
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
add a comment |
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
1
1
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
+1. Possible improvement: If we really don't trust what the remote server is providing, you might consider making the latter remove any NUL literals from the incoming JSON so hostile values can't split themselves into new elements. That said, that's a tiny quibble, and I'm not sure I've always consistently done it myself in every answer I have here.
– Charles Duffy
Dec 31 '18 at 14:45
add a comment |
PW8CoIlBJKbd,Owxu06PDmW2,Cv5vO1M,VR,2q02X6pTd2 V177jX1g2cFle LSU0NxGZk 4,WZcXALh0o7lqG0OGcU7KU5bAG NUdp
1
Converting a JSON object into a Bash associative array, How to convert string list to JSON string array in Bash?, etc.
– jww
Dec 31 '18 at 21:52