I need to parse a json with the following format

{
  "meta": {
    "rc": "ok"
  },
  "data": [
    {
      "average_wifi_utilization": {
        "total": 21.928571428571427,
        "na": 9.035714285714286,
        "ng": 34.82142857142857
      },
      "devices_status": {
        "uap": {
          "adopted": 28,
          "connected": 28,
          "disconnected": 0,
          "pending": 0,
          "disabled": 0
        },
        "usw": {
          "adopted": 2,
          "connected": 2,
          "disconnected": 0,
          "pending": 0
        },
        "ugw": {
          "adopted": 0,
          "connected": 0,
          "disconnected": 0,
          "pending": 0
        }
      },
      "wifi_score": {
        "client_score_avg": 81,
        "clients_with_poor_score": 8,
        "clients_with_fair_score": 3,
        "clients": 99,
        "retry_rate": 0,
        "signal": -61,
        "latency": 0
      }
    }
  ]
}

I need to extract value from "client_score_avg" key.

I'm able to extract first level of info with:

cat file.json | jq '.data'

However, when I try next step

cat file.json | jq '.data.[0]'

I get an error message:

jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:

I'm not sure what I'm doing wrong.

2 Answers

2
Inian On Best Solutions

I think you just need to do below. If your data[] array contains multiple entries, specify the index inside the [..] part. In your case, the below filter is same as specifying the index as 0 inside the [..]

jq '.data[].wifi_score.client_score_avg' file.json
0
Community On

here's an alternative solution based on a walk-path unix tool for JSON jtc:

bash $ <file.json jtc -w'<client_score_avg>l'
81
bash $ 

- it'll just find recursively the entry with label client_score_avg

PS> Disclosure: I'm the creator of the jtc tool