JSON – How to Grep and Print Value of a Key Stored in a Variable

jqjson

I have a variable with nested json,

a={
  "version": "3.0",
  "user": "unknown_unknown",
  "dateGenerated": "2020-07-08T11:53:23Z",
  "status": "OK",
  "data": [
    {
      "parameter": "t_2m:C",
      "coordinates": [
        {
          "lat": 39.23054,
          "lon": 9.11917,
          "dates": [
            {
              "date": "2020-07-08T15:53:23Z",
              "value": 25.1
            }
          ]
        }
      ]
    }
  ]
}

Looking for a way to grep the "value" in the nested json (like the one highlighted) in variable a.

I'am using grep and jq but I can't show value, it shows "dates"
(echo $result | grep -Po '"dates":.*?[^\\],.*?[^\\]"')
but not just the value.

Any help?

Best Answer

You want the "value" from (the first object in the "dates" array) from (the first object in the "coordinates" array) from (the first object in the "data" array)

$ a='{"version":"3.0","user":"unknown_unknown","dateGenerated":"2020-07-08T11:53:23Z","status":"OK","data":[{"parameter":"t_2m:C","coordinates":[{"lat":39.23054,"lon":9.11917,"dates":[{"date":"2020-07-08T15:53:23Z","value":25.1}]}]}]}'
$ echo "$a" | jq -r '.data[0].coordinates[0].dates[0].value'
25.1
Related Question