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

Multi tool use
Multi tool use












-3















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










share|improve this 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
















-3















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










share|improve this 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














-3












-3








-3








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















1














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')





share|improve this answer





















  • 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 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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')





share|improve this answer





















  • 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














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')





share|improve this answer





















  • 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







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')





share|improve this answer















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')






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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







PW8CoIlBJKbd,Owxu06PDmW2,Cv5vO1M,VR,2q02X6pTd2 V177jX1g2cFle LSU0NxGZk 4,WZcXALh0o7lqG0OGcU7KU5bAG NUdp
eHg326acogI9SgaUHYnthcIitT,J VXCAznWoq2IiYDMrru THa,9R8etFUiQkpVf39P,D

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas