首页 > 解决方案 > 在 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#

标签: jsonjq

解决方案


您可以简单地使用过滤器:

.Systems["@odata.id"]

也就是说,在 bash 或类似 bash 的提示符下,您会键入如下内容:

jq '.Systems["@odata.id"]' redfish.txt

推荐阅读