jq:无法使用字符串索引数组

Xu *_*ang 36 json jq

我在文件中有以下内容(我将其称为"myfile"):

[{
    "id": 123,
    "name": "John",
    "aux": [{
        "abc": "random",
        "def": "I want this"
    }],
    "blah": 23.11
}]
Run Code Online (Sandbox Code Playgroud)

我可以解析它没有[]如下:

$ cat myfile | jq -r '.aux[] | .def'
I want this
$
Run Code Online (Sandbox Code Playgroud)

但随着[]我得到:

$ cat myfile | jq -r '.aux[] | .def'
jq: error: Cannot index array with string
Run Code Online (Sandbox Code Playgroud)

我该如何处理[]使用jq?(我确信我可以使用不同的工具解析它们,但我想学习正确使用jq.

hek*_*mgl 65

它应该是:

jq '.[].aux[].def' file.json
Run Code Online (Sandbox Code Playgroud)

.[]迭代外部数组,.aux[]然后遍历aux每个节点的数组并.def打印其.def属性.

这将输出:

"I want this"
Run Code Online (Sandbox Code Playgroud)

如果你想摆脱双引号pass -r(--raw)jq:

jq -r '.[].aux[].def' file.json
Run Code Online (Sandbox Code Playgroud)

输出:

I want this
Run Code Online (Sandbox Code Playgroud)