Convert a JSON array to a bash array of strings

前端 未结 1 498
Happy的楠姐
Happy的楠姐 2020-12-01 19:18

How do I parse a json array of objects to a bash array with those objects as strings?

I am trying to do the following:

CONVO=$(get_json_array | jq \'         


        
相关标签:
1条回答
  • 2020-12-01 19:47

    The problem is that jq is still just outputting lines of text; you can't necessarily preserve each array element as a single unit. That said, as long as a newline is not a valid character in any object, you can still output each object on a separate line.

    get_json_array | jq -c '.[]' | while read object; do
        api_call "$object"
    done
    

    Of course, under that assumption, you could use the readarray command in bash 4 to build an array:

    readarray -t conversations < <(get_json_array | jq -c '.[]')
    for conversion in "${conversations[@]}"; do
        api_call "$conversation"
    done
    
    0 讨论(0)
提交回复
热议问题