【问题标题】:jq: error : Cannot iterate over null (null)jq:错误:无法迭代 null (null)
【发布时间】:2019-09-14 10:00:16
【问题描述】:

在下面的 Json 文件中,我试图提取“名称”:“abcd”、“版本”:“1.0.2”和“严重性”:“中”。

"status": "scanned",
    "data": {
        "Layer": {
            "IndexedByVersion": 3,
            "NamespaceName": "debian:9",
            "ParentName": "e762",
            "Name": ".4530bfac-5e99-4138-b071-4286c06669a3",
            "Features": [
                {
                    "Name": "openssl1.0",
                    "VersionFormat": "dpkg",
                    "NamespaceName": "debian:9",
                    "AddedBy": "85aa73fb8281cc252ed7e151f10386f36588ec6c967d45136103a4e1e705a81c.01bc7eff-9a5d-43f5-ab14-2e3e470cba77",
                    "Version": "1.0.2q-1~deb9u1",
                    "Vulnerabilities": [
                        {
                            "Severity": "Medium",
                            "NamespaceName": "debian:9",
                            "Link": "xxxx",
                            "FixedBy": "1.0.2r-1~deb9u1",
                            "Description": " n must call SSL_shutdown() twice even if a protocol error has occurred (applications should not do this but some do anyway). Fixed in OpenSSL 1.0.2r (Affected 1.0.2-1.0.2q).",
                            "Name": "CVE-2019-1559",
                            "Metadata": {
                                "NVD": {
                                    "CVSSv2": {
                                        "Score": 4.3,
                                        "Vectors": "AV:N/AC:M/Au:N/C:P/I:N"
                                    }
                                }
                            }
                        }
                    ]
                },
                {
                    "VersionFormat": "dpkg",
                    "NamespaceName": "debian:9",
                    "Version": "0.16-1+deb9u1",
                    "Name": "libidn2-0",
                    "AddedBy": "85aa73fb8281cc252ed7e151f10386f36588ec6c967d45136103a4e1e705a81c.01bc7eff-9a5d-43f5-ab14-2e3e470cba77"
                },
                {
                    "VersionFormat": "dpkg",
                    "NamespaceName": "debian:9",
                    "Version": "0.29-4",
                    "Name": "pkg-config",
                    "AddedBy": "4d2169f1dc7652ffd2a4f32d2c0ae2
                },


                {
                    "Name": "nettle",
                    "VersionFormat": "dpkg",
                    "NamespaceName": "debian:9",
                    "AddedBy": "7494d6c991278b43e8388f7cec2f138075
                    "Version": "3.3-1",
                    "Vulnerabilities": [
                        {
                            "Severity": "Low",
                            "NamespaceName": "debian:9",
                            "Link": "xxxx",
                            "Description": "er.",
                            "Name": "CVE-2018-16869",
                            "Metadata": {
                                "NVD": {
                                    "CVSSv2": {
                                        "Score": 3.3,
                                        "Vectors": ":P"
                                    }

到目前为止,我可以使用下面的 jq 命令提取名称和版本的值。

jq -r '.data.Layer| .Features[] | "\(.Name) \(.Version)"' status.json

但是当我尝试使用以下命令提取“严重性”字段的值时

`jq -r '.data.Layer| .Features[] | "\(.Name) \(.Version)"| .Vulnerabilities[].Severity' status.json`

我在标题中收到错误消息。

Required output: abcd 12.0 medium

非常感谢任何帮助。

【问题讨论】:

  • 您能否将问题中的示例文档扩展为minimal reproducible example?现在,输入文档的示例子集不符合语法(打开它不会关闭的结构),不是最小的(包括与输出无关的内容),并且不执行失败案例(你想要忽略没有漏洞的项目,但不显示任何漏洞)。

标签: json jq git-bash


【解决方案1】:

稍微改变输出格式:

jq -r '.data.Layer| .Features[] | .Name ,.Version, .Vulnerabilities[].Severity' input

但这也有效:

jq -r '.data.Layer| .Features[] | "\(.Name)  \(.Version)  \(.Vulnerabilities[].Severity)"' input

【讨论】:

  • 感谢您的快速响应,但我在提取字段时遇到了另一个问题。我刚刚编辑了源 JSON 文件。我需要获取“漏洞”字段不为 NULL/空的字段/键(名称、版本、严重性)值。
【解决方案2】:

如果可以接受替代解决方案,让我为您提供一个基于 JSON 步行路径 unix 工具的解决方案:jtc(将您对 William 的评论接受):

这样,您将收集 NameVersionSeverity 仅谓词 Severity 记录存在于 Vulnerabilities 中(显然,如果存在 Vulnerabilities 记录):

bash $ <status.json jtc -x'<Features>l[:][Vulnerabilities]<Severity>l[^4]' -y'[Name]' -y'[Version]' -y'<Severity>l' 
"openssl1.0"
"1.0.2q-1~deb9u1"
"Medium"
"nettle"
"3.3-1"
"Low"
bash $ 

而且,如果您想对每一行的内容进行分组,请将其通过管道发送到 xargspaste

bash $ <status.json jtc -x'<Features>l[:][Vulnerabilities]<Severity>l[^4]' -y'[Name]' -y'[Version]' -y'<Severity>l' | xargs -L3
openssl1.0 1.0.2q-1~deb9u1 Medium
nettle 3.3-1 Low
bash $ 

披露:我是jtc 工具的创建者

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2015-03-28
    • 2019-08-04
    • 2019-06-08
    • 2020-12-21
    • 1970-01-01
    • 2015-01-12
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多