Home > Enterprise >  Bash jq with dynamic name
Bash jq with dynamic name

Time:04-20

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?

CodePudding user response:

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 using ..:

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

Online demo


Both return 200

CodePudding user response:

Thanks I solved. This is my solution:

jq -r '.data | .[]."http_httpCode#HTTP Code"'
  • Related