jq:错误:无法遍历字符串 [英] jq: error: Cannot iterate over string
问题描述
有人可以解释以下警告吗?
could someone please explain below warning?
输入文件为:
{
"env": "DC",
"hosts" :
[
{
"apt_update_last_success": "1495991703",
"architecture": "amd64",
"hostname": "h1"
},
{
"apt_update_last_success": "1495991703",
"architecture": "amd64",
"hostname": "h2"
},
{
"apt_update_last_success": "1496045706",
"architecture": "amd64",
"hostname": "h3"
},
{
"apt_update_last_success": "1496045705",
"architecture": "amd64",
"hostname": "h4"
},
{
"apt_update_last_success": "1496049305",
"architecture": "amd64",
"hostname": "h5"
},
{
"apt_update_last_success": "1496049307",
"architecture": "amd64",
"hostname": "h6"
}
]
}
jq命令返回预期的结果,但还会显示我不知道为什么的警告:
the jq command returns what expected but also prints the warning I dont know why:
$ jq -r '.[][] | select(.hostname=="h6")' ddd.json
jq: error: Cannot iterate over string
{
"apt_update_last_success": "1496049307",
"architecture": "amd64",
"hostname": "h6"
}
请告诉我如何摆脱这种情况.
please tell me how to get rid of this.
谢谢.
推荐答案
问题出在您的符号.[][]
中.您的输入只是一个 对象 ,但是您试图将其表示为容器" .[][]
的容器".
The issue is in your notation .[][]
. Your input is just an object but you are trying to present it as a "container" of "containers" .[][]
.
正确的方法是:
jq '.hosts[] | select(.hostname=="h6")' ddd.json
{
"apt_update_last_success": "1496049307",
"architecture": "amd64",
"hostname": "h6"
}
此外,在 jq 1.5 上,此jq -r '.[][] | select(.hostname=="h6")' ddd.json
不会返回预期的对象,而只会打印
jq: error (at jq1:36): Cannot iterate over string ("DC")
Besides, on jq 1.5 this jq -r '.[][] | select(.hostname=="h6")' ddd.json
doesn't return the expected object but only prints
jq: error (at jq1:36): Cannot iterate over string ("DC")
这篇关于jq:错误:无法遍历字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!