我在文件中有以下内容(我将其称为"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)
归档时间: |
|
查看次数: |
49789 次 |
最近记录: |