0

I tried echo $json | jq '.result.hostid' but that returns error.

And what if I have to get the interface array belonging to a particular hostid?

Example: jq '.result[].hostid == 10084 | .result[].interface' obviously this doesn't work but I guess that explains my question? Basically return interface array for the hostid specified.

{
  "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"
R0bert2
  • 47
  • 5
  • `result` is an array. it should be `.result[].hostid` – Inian Apr 28 '20 at 14:05
  • And what if I have to get the interface array belonging to a particular hostid? I kinda tried with map but it didn't work – R0bert2 Apr 28 '20 at 14:14
  • Use jq select like this: `jq '.result[] | select(.hostid == "10084")'` – 0stone0 Apr 28 '20 at 14:53
  • To get the interface array just add `| .interfaces` to the end of [0stone0](https://stackoverflow.com/users/5625547/0stone0)'s suggestion e.g. `.result[] | select(.hostid == "10084") | .interfaces` [Try it online!](https://tio.run/##VY3BCsMgDIbvPkXIaYMisQw2Bn2S4mE4x1pK26k7dX12p7WzHUI0/xe/tC/vudH23blawges7rRyB/4crGvuUFWAguhywmOAvOmdNo@b0tb7iQFga4fejAqvgCUnLGKWbCGqQwcwLTXkSRlHk7HYgxjHm0hsYFuXbXvj/8yqzr8THZewPHMKR2Bm8/qS7NdJNn8B "jq – Try It Online") – jq170727 Apr 28 '20 at 16:34
  • That worked thanks – R0bert2 Apr 29 '20 at 05:18

0 Answers0