Skip to content
Advertisement

Bash jq with dynamic name

I’m trying to create a script to read the return code from a json from a curl command.

my curl command is:

curl -sk 'https://192.168.0.1/custom/getData.php?device=mydevice&object=http--HTTP_v6_Global Index&indicator=http_httpCode&plugin=xxx' | jq '.'

The json output is:

{
  "device": "mydevice",
  "object": "http--HTTP_v6_Global ",
  "object_descr": "HTTP download of http://192.168.0.1",
  "indicator": "http_httpCode",
  "indicator_descr": null,
  "plugin": "xxx",
  "starttime": 1650468121,
  "endtime": 1650468421,
  "data": {
    "1650468248": {
      "http_httpCode#HTTP Code": 200
    }
  }
}

How can read the value "http_httpCode#HTTP Code" if 1650468248 is a dynamic value?

Advertisement

Answer

You could use to_entries so you can use .value to target the ‘unknown’ key:

jq '.data | to_entries | first | .value."http_httpCode#HTTP Code"'

Online demo


Another approach by just ‘looping over everything’ either with .. or .[]:

jq '.data | .. | ."http_httpCode#HTTP Code"? // empty'

Online demo

jq '.data | .[]."http_httpCode#HTTP Code"'

Online demo


They all return 200

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement