jq:无法使用字符串索引数组 [英] jq: Cannot index array with string

查看:202
本文介绍了jq:无法使用字符串索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following in a file (which I will call "myfile"):

[{
    "id": 123,
    "name": "John",
    "aux": [{
        "abc": "random",
        "def": "I want this"
    }],
    "blah": 23.11
}]

我可以在不使用[]的情况下进行解析,如下所示:

I can parse it without the [ and ] as follows:

$ cat myfile | jq -r '.aux[] | .def'
I want this
$

但使用[]我得到:

$ cat myfile | jq -r '.aux[] | .def'
jq: error: Cannot index array with string

如何使用jq处理[]? (我确定我可以使用其他工具来解析它们,但我想学习jq的正确用法.

How can I deal with the [ and ] using jq? (I'm sure I could parse them off with a different tool but I want to learn correct usage of jq.

推荐答案

应为:

jq '.[].aux[].def' file.json

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

.[] iterates over the outer array, .aux[] then iterates over the the aux array of every node and .def prints their .def property.

这将输出:

"I want this"

如果要消除双引号,请将-r(--raw)传递给jq:

If you want to get rid of the double quotes pass -r (--raw) to jq:

jq -r '.[].aux[].def' file.json

输出:

I want this

这篇关于jq:无法使用字符串索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆