JSON-如何使用JQ打印特定值

时间:2020-04-28 14:04:19

标签: json jq

我尝试了echo $json | jq '.result.hostid',但返回错误。

如果我必须获取属于特定hostid的接口数组怎么办?

示例:jq '.result[].hostid == 10084 | .result[].interface'显然不起作用,但是我想这解释了我的问题?基本上返回指定的hostid的接口数组。

{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10084",
      "host": "host001",
      "interfaces": [
        {
          "interfaceid": "1",
          "ip": "127.0.0.1"
        }
      ]
    }
  ]
}

root @ host001:〜#echo $json | jq -r '.result.hostid'

jq: error (at <stdin>:1): Cannot index array with string "hostid"

0 个答案:

没有答案