【问题标题】:jq: error: Cannot iterate over stringjq:错误:无法遍历字符串
【发布时间】:2018-04-11 07:09:00
【问题描述】:

有人可以解释下面的警告吗?

输入文件是:

{
 "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 命令返回预期的结果,但也打印出我不知道为什么的警告:

$ jq -r '.[][] | select(.hostname=="h6")' ddd.json
jq: error: Cannot iterate over string
{
  "apt_update_last_success": "1496049307",
  "architecture": "amd64",
  "hostname": "h6"
}

请告诉我如何摆脱它。

谢谢。

【问题讨论】:

    标签: jq


    【解决方案1】:

    问题在于您的符号.[][]。您的输入只是一个对象,但您正试图将其呈现为“容器”.[][] 的“容器”。

    正确的做法是:

    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")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      相关资源
      最近更新 更多