'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?
Solution 1:[1]
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"'
Another approach by just 'looping over everything' either with .. or .[]:
jq '.data | .. | ."http_httpCode#HTTP Code"? // empty'
jq '.data | .[]."http_httpCode#HTTP Code"'
They all return 200
Solution 2:[2]
Thanks I solved. This is my solution:
jq -r '.data | .[]."http_httpCode#HTTP Code"'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Marco Ferrara |
