简体   繁体   English

Bash jq 动态名称

[英]Bash jq with dynamic name

I'm trying to create a script to read the return code from a json from a curl command.我正在尝试创建一个脚本来从 curl 命令读取 json 的返回码。

my curl command is:我的 curl 命令是:

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: json output 是:

{
  "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?如果1650468248是动态值,如何读取值"http_httpCode#HTTP Code"

You could use to_entries so you can use .value to target the 'unknown' key:您可以使用to_entries以便您可以使用.value来定位“未知”键:

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他们都返回200

Thanks I solved.谢谢我解决了。 This is my solution:这是我的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM