使用 JQ 从列表项具有特定/已知值的 JSON 中检索数据



我有一个 JSON 文件,其中包含如下块

{
"id": 0000,
"sGName": "SG1",
"customProperties": [
{
"id": 100000,
"name": "clustersIP",
"value": "ABC"
}
],
"filters": [
{
"id": 74616,
"attributeName": "serverName",
"value": "Sever101"
},
{
"id": 74617,
"attributeName": "bigIPPool",
"value": "server101v1"
}
],
},

我正在寻找 JQ 在 bash 中检索 sGName,其中"属性名称"="服务器名称","值"="Sever101" 有人可以帮忙吗?

我可能有多个"sGName">

我相信您正在寻找以下命令:

jq --raw-output 'map( select(.filters | any(.attributeName == "serverName" and .value == "Sever101")) |.sGName) | join("n")'

你可以在这里尝试。

将服务器名称条件提取为参数:

jq --raw-output --arg serverName "Sever101" 'map( select(.filters | any(.attributeName == "serverName" and .value == $serverName)) |.sGName) | join("n")'

Oguz Ismail提出了一个更简化的解决方案:

jq --raw-output --arg serverName "Sever101" '.[] | select(any(.filters[]; .attributeName=="serverName" and .value==$serverName)).sGName'

最新更新