2

I have an empty output.json and I want to populate it with {key, value} pairs where key is a string and value is a Json array read from file. I need to go through this for multiple files to populate the output.json. So far, value is being populated successfuly.

$ jq --argjson cves "$(cat my-scan-result-N.json)" '.+={"TODO": $cves}' output.json
{
  "TODO": [
    {
      "cvePK": "CVE-2020-11656",
      "summary": "In SQLite through 3.31.1, the ALTER TABLE implementation has a use-after-free, as demonstrated by an ORDER BY clause that belongs to a compound SELECT statement.",
      "cvss": 7.5,
      "notes": ""
    },
    {
      "cvePK": "CVE-2019-19646",
      "summary": "pragma.c in SQLite through 3.30.1 mishandles NOT NULL in an integrity_check PRAGMA command in certain cases of generated columns.",
      "cvss": 7.5,
      "notes": ""
    }
  ]
}

However, when I add another --argjson to populate the key ("TODO") with desired value $FQDN, it fails with an error.

$ FQIN="example.com/foo/bar:7.0.3"  # Tried \""example.com/foo/bar:7.0.3"\" as well but doesn't work.

$ jq --argjson cves "$(cat my-scan-result.json)" --argjson fqin="FQIN" '.+={$fqin: $cves}' output.json


C:\ProgramData\chocolatey\lib\jq\tools\jq.exe: invalid JSON text passed to --argjson
Use C:\ProgramData\chocolatey\lib\jq\tools\jq.exe --help for help with command-line options,
or see the jq manpage, or online docs  at https://stedolan.github.io/jq

So my goal is to have something like below, but above error message is not helpful enough. Any help would be appreciated.

{
  "example.com/foo/bar:7.0.3": [
    {
      "cvePK": "CVE-2020-11656",
      "summary": "In SQLite through 3.31.1, the ALTER TABLE implementation has a use-after-free, as demonstrated by an ORDER BY clause that belongs to a compound SELECT statement.",
      "cvss": 7.5,
      "notes": ""
    },
    {
      "cvePK": "CVE-2019-19646",
      "summary": "pragma.c in SQLite through 3.30.1 mishandles NOT NULL in an integrity_check PRAGMA command in certain cases of generated columns.",
      "cvss": 7.5,
      "notes": ""
    }
  ]
}   

2 Answers 2

5

The line:

jq --argjson cves "$(cat my-scan-result.json)" --argjson fqin="FQIN" '.+={$fqin: $cves}' output.json

has several errors:

  1. The phrase --argjson fqin="FQIN" is incorrect. Please see the jq manual for details. Suffice it to say here that you could achieve the desired effect by writing --arg fqin "$FQIN".

  2. The jq expression {$fqin: $cves} is incorrect. When a key name is specified using a variable, the variable must be enclosed in parentheses: {($fqin): $cves}. (Indeed, whenever the key name is specified indirectly, the specifying expression must be enclosed in parentheses.)

Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome. 1. Yes, the syntax was wrong. I overlooked at the syntax.<br/> 2. (${fqin}): and this was the culprit whole time. Much appreciated your help, @peak!
0

You can do

FQIN="example.com/foo/bar:7.0." jq -n --argjson cves "$(cat my-scan-result.json)" '.+={(env.FQIN): $cves}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.