我有以下的JSON文件:
{
"data": {
"allPost": {
"edges": [
{
"node": {
"slug": "fp-cheat-sheet",
"labels": [
"FUNCTIONAL PROGRAMMING",
"HASKELL",
"SCALA"
]
}
},
{
"node": {
"slug": "nlp-101",
"labels": [
"DATA SCIENCE",
"NLP",
"PYTHON"
]
}
}
]
}
}
}
我正在使用JQ编写一个Shell脚本来提取每个
edge
的信息。从每个edge
(一个数组),我能够提取slugs
,它是一个字符串,但无法提取另一个数组labels
。readarray -t slugs < <(cat ${json_file_name} | jq .data.allPost.edges[].node.slug)
readarray -t tags < <(cat ${json_file_name} | jq .data.allPost.edges[].node.labels)
echo ${slugs[1]}
# successfully prints '"nlp-101"'
echo ${tags[1]}
# wrongly prints '"FUNCTIONAL PROGRAMMING",'
我希望提取每篇文章的标签,并以相同的格式将其打印在文件中。例如,对于
echo ${tags[1]}
,我希望它打印出: ["DATA SCIENCE", "NLP", "PYTHON"]
如何修复上述代码以按预期从上述JSON中提取标签?