chuong trung vuong
chuong trung vuong

Reputation: 13

jq | to return specific item in nested json object

I am new to jq and cannot work out how to get idle value from the above json. By using jq command to retrieve json. How do I get idle value using jq?

{
  "sysstat": {
    "hosts": [
      {
        "nodename": "localhost.localdomain",
        "sysname": "Linux",
        "release": "4.13.13-200.fc26.x86_64",
        "machine": "x86_64",
        "number-of-cpus": 2,
        "date": "24/11/17",
        "statistics": [
          {
            "timestamp": "00:00:45",
            "cpu-load": [
              {
                "cpu": "0",
                "usr": 5.08,
                "nice": 0.03,
                "sys": 1.31,
                "iowait": 0.07,
                "irq": 0.73,
                "soft": 0.09,
                "steal": 0,
                "guest": 0,
                "gnice": 0,
                "idle": 92.69
              }
            ]
          }
        ]
      }
    ]
  }
}

Upvotes: 1

Views: 791

Answers (2)

peak
peak

Reputation: 116780

In this particular case, a short-and-sweet solution is:

.. | .idle? // empty

If the location of "idle" is important, then in case it appears more than once (or not at all), you may wish to consider:

.sysstat.hosts[].statistics[] | .["cpu-load"][] | .idle

Upvotes: 2

jq170727
jq170727

Reputation: 14665

Here is one solution, assuming the data is in data.json:

$ jq -M '.sysstat.hosts[0].statistics[0]."cpu-load"[0].idle' data.json
92.69

Try it online at jqplay.org

Upvotes: 2

Related Questions