您好,我有以下 json 并尝试获取所有拥有“linux-image-amd64”的父母
{
"web-prode-01.example.de": "ERROR: Problem encountered installing package(s). Additional info follows:\n\nchanges:\n ----------\n linux-image-amd64:\n ----------\n new:\n old:\n 6.1.55-1\nerrors:\n - Running scope as unit: run-rad31bddd35ec452b9d67fef004ce3daf.scope\n E: Sub-process /usr/bin/dpkg returned an error code (1)",
"web-prode-04.example.de": {
"linux-image-amd64": {
"old": "5.10.197-1",
"new": "5.10.205-2"
},
"linux-image-5.10.0-27-amd64": {
"old": "",
"new": "5.10.205-2"
}
},
"lmrelaunch.example.de": {
"linux-image-6.1.0-0.deb11.13-amd64": {
"old": "",
"new": "6.1.55-1~bpo11+1"
},
"linux-image-amd64": {
"old": "6.1.38-4~bpo11+1",
"new": "6.1.55-1~bpo11+1"
}
}
}
我尝试了
jq -r 'keys[] as $k | "\($k) \(.[$k]."linux-image-amd64")"'
,但我只得到了一个条目而不是两个
to_entries[] | select(.value | type == "object" and has("linux-image-amd64")) | .key
退货:
"web-prode-04.example.de"
"lmrelaunch.example.de"
您应该检查上下文是否
has
该键。另外,为了防止字符串在尝试应用 has
时失败,可以过滤 objects
,或者使用 ?
抑制错误。无论哪种情况,请使用此条件使用 select
过滤结果。
keys[] as $k | .[$k] | objects | select(has("linux-image-amd64"))
# or
keys[] as $k | .[$k] | select(has("linux-image-amd64")?)
{
"linux-image-6.1.0-0.deb11.13-amd64": {
"old": "",
"new": "6.1.55-1~bpo11+1"
},
"linux-image-amd64": {
"old": "6.1.38-4~bpo11+1",
"new": "6.1.55-1~bpo11+1"
}
}
{
"linux-image-amd64": {
"old": "5.10.197-1",
"new": "5.10.205-2"
},
"linux-image-5.10.0-27-amd64": {
"old": "",
"new": "5.10.205-2"
}
}
然后您可以使用
$k
构建输出,例如:
keys[] as $k | .[$k] | select(has("linux-image-amd64")?) | $k
lmrelaunch.example.de
web-prode-04.example.de