json jq如何获取特定孩子的所有父母

ojsjcaue  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(155)

你好,我有下面的json,并试图让所有的父母谁拥有“linux-image-amd 64”

{
    "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")"',但我只得到一个条目,而不是两个

ca1c2owp

ca1c2owp1#

您应该检查上下文has是否是该键。此外,为了防止字符串在尝试对其应用has时失败,请过滤objects,或使用?抑制错误。在任何一种情况下,使用此条件使用select过滤结果。

keys[] as $k | .[$k] | objects | select(has("linux-image-amd64"))
# or
keys[] as $k | .[$k] | select(has("linux-image-amd64")?)

个字符
然后,您可以使用$k构建输出,例如:

keys[] as $k | .[$k] | select(has("linux-image-amd64")?) | $k
lmrelaunch.example.de
web-prode-04.example.de

的字符串

xuo3flqw

xuo3flqw2#

to_entries[] | select(.value | type == "object" and has("linux-image-amd64")) | .key

字符串
工作演示在这里
回报率:

"web-prode-04.example.de"
"lmrelaunch.example.de"

相关问题