jq: 无法用字符串索引数组
jq: Cannot index array with string
我在一个文件中有以下内容(我称之为“我的文件”):
[{
"id": 123,
"name": "John",
"aux": [{
"abc": "random",
"def": "I want this"
}],
"blah": 23.11
}]
如果文件没有第一个 [
和最后一个 ]
,我可以解析它,如下所示:
$ cat myfile | jq -r '.aux[] | .def'
I want this
$
但使用 [
和 ]
我得到:
$ cat myfile | jq -r '.aux[] | .def'
jq: error: Cannot index array with string
如何使用 jq 处理 [
和 ]
? (我确定我可以用不同的工具解析它们,但我想学习 jq 的正确用法。
应该是:
jq '.[].aux[].def' file.json
.[]
遍历外部数组,.aux[]
然后遍历每个节点的 aux
数组,.def
打印它们的 .def
属性.
这将输出:
"I want this"
如果你想摆脱双引号传递 -r
(--raw
) 到 jq
:
jq -r '.[].aux[].def' file.json
输出:
I want this
我在一个文件中有以下内容(我称之为“我的文件”):
[{
"id": 123,
"name": "John",
"aux": [{
"abc": "random",
"def": "I want this"
}],
"blah": 23.11
}]
如果文件没有第一个 [
和最后一个 ]
,我可以解析它,如下所示:
$ cat myfile | jq -r '.aux[] | .def'
I want this
$
但使用 [
和 ]
我得到:
$ cat myfile | jq -r '.aux[] | .def'
jq: error: Cannot index array with string
如何使用 jq 处理 [
和 ]
? (我确定我可以用不同的工具解析它们,但我想学习 jq 的正确用法。
应该是:
jq '.[].aux[].def' file.json
.[]
遍历外部数组,.aux[]
然后遍历每个节点的 aux
数组,.def
打印它们的 .def
属性.
这将输出:
"I want this"
如果你想摆脱双引号传递 -r
(--raw
) 到 jq
:
jq -r '.[].aux[].def' file.json
输出:
I want this