json - 在 Redfish 输出中使用 jq 检索 @odata.id 的值
问题描述
我正在使用 jq 解析一些 json Redfish 数据。
尝试从下面的 redfish.txt 文件中提取 @odata.id 的值。
使用推荐的 jq 调用 .["@odata.id"] 似乎并不能很好地提取值本身,即:/redfish/v1/Systems
欢迎任何建议。下面的输出... :)
谢谢,尼克
root@ubuntu-xenial:/var/opt# cat redfish.txt
{"@odata.context":"/redfish/v1/$metadata#ServiceRoot.ServiceRoot","@odata.id":"/redfish/v1","@odata.type":"#ServiceRoot.v1_2_0.ServiceRoot","AccountService":{"@odata.id":"/redfish/v1/Managers/iDRAC.Embedded.1/AccountService"},"Chassis":{"@odata.id":"/redfish/v1/Chassis"},"Description":"Root Service","EventService":{"@odata.id":"/redfish/v1/EventService"},"Id":"RootService","JsonSchemas":{"@odata.id":"/redfish/v1/JSONSchemas"},"Links":{"Sessions":{"@odata.id":"/redfish/v1/Sessions"}},"Managers":{"@odata.id":"/redfish/v1/Managers"},"Name":"Root Service","Oem":{"Dell":{"@odata.type":"#DellServiceRoot.v1_0_0.ServiceRootSummary","IsBranded":0,"ManagerMACAddress":"d0:96:69:51:d4:70","ServiceTag":"XXXX"}},"RedfishVersion":"1.2.0","Registries":{"@odata.id":"/redfish/v1/Registries"},"SessionService":{"@odata.id":"/redfish/v1/SessionService"},"Systems":{"@odata.id":"/redfish/v1/Systems"},"Tasks":{"@odata.id":"/redfish/v1/TaskService"},"UpdateService":{"@odata.id":"/redfish/v1/UpdateService"}}
root@ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems
{
"@odata.id": "/redfish/v1/Systems"
}
root@ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .@odata.id
jq: error: syntax error, unexpected FIELD, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.@odata.id
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.@odata.id
jq: 2 compile errors
root@ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .["@odata.id"]
{
"@odata.id": "/redfish/v1/Systems"
}
"/redfish/v1/Systems"
root@ubuntu-xenial:/var/opt# cat redfish.txt | jq .Systems | jq .["odata.id"]
{
"@odata.id": "/redfish/v1/Systems"
}
"/redfish/v1/Systems"
root@ubuntu-xenial:/var/opt#
解决方案
您可以简单地使用过滤器:
.Systems["@odata.id"]
也就是说,在 bash 或类似 bash 的提示符下,您会键入如下内容:
jq '.Systems["@odata.id"]' redfish.txt
推荐阅读
- c - 下面的例子中预处理器是如何工作的?
- kotlin - 具有枚举约束的 Kotlin 泛型函数
- suitecrm - SuiteCRM 详细信息视图和创建视图不显示任何内容
- python - 在python中的另一个字典列表中搜索列表字典的键值
- python - jupyter notebook (python 3.7) 导入失败
- html - 用 img 替换 bootstrap-5 svg 占位符
- git - 带有 git 的 Qt 文件
- angular - formControlName 指令和 FormArray 问题
- xargs - “find and xargs”没有按预期工作:如何从 xargs 中获取 arg 的基本名称?
- azure-devops - Azure Pipeline 在运行被取消或成功时执行清理工作