0

The output of my bash command is something like this

["Name1","Name2"]

I need to parse this information into an array with Name1 being the array value 1 and Name2 being the array value 2.

I have tried doing the following:

var1="/bin/curl http://localhost:8083/names"
$var1

yields the following output:

["Name1","Name2"]

And then I tried this to convert the var1 variable into an array

my_array=( $(var1) )

It doesn't work.

After putting the results into var1, I need to convert them into array. How can I do that?

0

5 Answers 5

1

Assuming none of the names can contain a newline,

get_names () {
    curl http://localhost:8083/names
}

readarray -a my_array < <(get_names | jq -r '.[]')
Sign up to request clarification or add additional context in comments.

Comments

1

To be safe with values containing special characters (newlines, tabs...) and parsing your server's answer with jq. You can use a null delimited output:

#!/usr/bin/env bash

get_names() {
  curl http://localhost:8083/hames
}

# fill my_array from null delimited values returned by jq
mapfile -d '' my_array < <(
  get_names |
    jq --join-output '.[]+"\u0000"' # output null delimited array values
)

# debug print my_array values
for i in "${!my_array[@]}"; do
  printf 'my_array[%d]=%q\n' "$i" "${my_array[$i]}"
done

2 Comments

Got the following error while running the script mapfile: -a: invalid option mapfile: usage: mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
There is no mapfile -a in my script above. Maybe it was meant for someone else's answer
0

You could do some character fiddling and try to extract the strings like that, but it would be brittle. Instead, I recommend you use a tool that can parse JSON such as jq:

read -d '\t' -a my_array <<< "$(curl http://localhost:8083/names | jq -r '@tsv')"

This reads a tab separated line, as produced by the @tsv filter, into the my_array array.

2 Comments

How do I access those elements individually ?
@KarthickGanesan my_array is now an array: echo "${my_array[0]}", echo "${my_array[1]}" etc. for individual elements.
0

This worked for me

var1='["Name1","Name2"]'
my_array=( $( echo '["Name1","Name2"]' | sed 's/[][,]/ /g') )
echo ${my_array[@]}
echo "${my_array[1]}"
echo "${my_array[0]}"

output

"Name1" "Name2"
"Name2" 
"Name1"

IHTH

3 Comments

The ["Name1",...."Name-n"]. The output will be varying. In this case what should I do. To put them into an array regardless of their count. The count is dynamic
You wrote "I need to convert them into array.", As long as var1 contains all the values you need, then you can then access the count with echo "${#my_array[@]}" and reference individual elements like i=$(( ${#my_array[@)} -- )) ; echo "${my_array[$i]}". If you have further questions about this technique, please post a new question that includes code that isn't working, a set of data that covers your problem, the required output from that data, and your current output from your code and any error messages that appear (actual text, not just "it's not working"). please. Good luck.
Hmm, correction on the use of indices with arrays : i=${#my_array[@]}; echo "i=$i" ; (( i-- )) ; echo "i=$i" ; echo ${my_array[$i]}
0

You can also try the below approach:

arr=( $(echo '["Name1","Name2"]' | sed 's/[][]//g' | sed 's/"//g') )
for i in "${arr[@]}"; do echo $i; done

Here, replace echo '["Name1","Name2"]' with echo $var1, so it would be like:

arr=( $(echo $var1 | sed 's/[][]//g' | sed 's/"//g') )
for i in "${arr[@]}"; do echo $i; done

echo "${arr[0]}" # will hold the value Name1
echo "${arr[1]}" # will hold the value Name2

2 Comments

The ["Name1",...."Name-n"]. The output will be varying. In this case what should I do. To put them into an array regardless of their count.
All values should be available in the array named arr. You can iterate the array as shown above. You know what I mean; for i in "${arr[@]}"; do echo $i; done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.