用jq获取数组的所有值

时间:2017-08-05 15:12:48

标签: json parsing jq

我使用jq:

解析json文件
np.uint8

它工作正常,但每次我必须输入数字.response [2] .text,.response [3] .text等。我想一次获得所有值(200个值)

但是当我这样做时:

jq .response[1].text file.json

它出错:无法用字符串“text”索引数字

该文件如下所示:

jq .response[].text file.json

1 个答案:

答案 0 :(得分:6)

显然,数组中的一个项是字符串。如果你的jq支持“?”,那么有一种可能性就是使用它:

.response[].text?

另一种方法是明确检查类型,例如:

.response[] | objects | .text

又一种可能性:

.response[] | select(type=="object" and has("text")) | .text

如果您希望在没有“text”字段时拥有占位符值:

 .response[] | if type=="object" and has("text") then .text else null end

因此,这实际上取决于您的要求以及您正在使用的jq版本。

相关问题