Gowtham Ramamoorthy
Gowtham Ramamoorthy

Reputation: 896

jq: error : Cannot iterate over null (null)

In the below Json file, I am trying to extract "Name": "abcd","Version": "1.0.2" and "Severity": "Medium".

"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"
                                    }

So far I am able to extract the values of Name & version using the jq command below.

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

but when I try to extract values of "Severity" field using the below command

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

I get the error message in the title.

Required output: abcd 12.0 medium

Any help is much appreciated.

Upvotes: 0

Views: 7955

Answers (3)

Nick
Nick

Reputation: 1

If you use Vulnerabilities[]? instead of [] jq will just skip the Features which don't have a Vulnerability.

Upvotes: 0

Dmitry
Dmitry

Reputation: 1293

if an alternative solution is acceptable, let me offer you a solution based on a walk-path unix tool for JSON: jtc (accommodating your comment to William):

This way, you'll collect Name, Version and Severity only predicated Severity record is present in Vulnerabilities (and, obviously, if Vulnerabilities record exists):

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 $ 

And, if you like to group those on each line, pipe it to xargs, or paste:

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 $ 

Disclosure: I'm the creator of jtc tool

Upvotes: 1

William Pursell
William Pursell

Reputation: 212356

Changes the output format slightly:

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

But this also works:

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

Upvotes: 3

Related Questions