Skip to main content
1 of 5
Philip Kirkbride
  • 10.8k
  • 33
  • 107
  • 176

Convert Python/JavaScript Style Array to Bash

I'm using JQ to fetch some JSON from a quiz database and I want to parse the results. I'm trying to save a resulting array in Bash as shown below but the format is that used in JavaScript/Python with square brackets rather than Bash style.

quiz=$(curl -s https://opentdb.com/api.php?amount=3)
answers=$(echo $quiz | jq '[ .results][0][0].incorrect_answers')
correct=$(echo $quiz | jq '[ .results][0][0].correct_answer')
answers+=( $correct )

An example of what answers looks like is below:

[ "Excitement", "Aggression", "Exhaustion" ]

The correct answer is never pushed to the array due to the wrong format.

How can I convert an array of the format shown above so that it is interpreted in my script as an array.

Philip Kirkbride
  • 10.8k
  • 33
  • 107
  • 176