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.
Example of output from curl
(this is not hard-coded, question and answer is different every-time):
{
"response_code": 0,
"results": [
{
"category": "Entertainment: Television",
"type": "multiple",
"difficulty": "easy",
"question": "Which company has exclusive rights to air episodes of the "The Grand Tour"?",
"correct_answer": "Amazon",
"incorrect_answers": [
"Netflix",
"BBC",
"CCTV"
]
},
{
"category": "Celebrities",
"type": "multiple",
"difficulty": "medium",
"question": "How much weight did Chris Pratt lose for his role as Star-Lord in "Guardians of the Galaxy"?",
"correct_answer": "60 lbs",
"incorrect_answers": [
"30 lbs",
"50 lbs",
"70 lbs"
]
},
{
"category": "Animals",
"type": "boolean",
"difficulty": "easy",
"question": "The Killer Whale is considered a type of dolphin.",
"correct_answer": "True",
"incorrect_answers": [
"False"
]
}
]
}
correct_answer
andincorrect_answers
.