nanaboo
nanaboo

Reputation: 395

how to add an object to existing json file using jq

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

Upvotes: 2

Views: 2967

Answers (2)

Logan Lee
Logan Lee

Reputation: 977

You can do

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

Upvotes: 0

peak
peak

Reputation: 116640

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.)

Upvotes: 5

Related Questions