как добавить объект в существующий json файл с помощью jq - PullRequest
1 голос
/ 18 июня 2020

У меня пустой вывод. json и я хочу заполнить его парами {ключ, значение}, где ключ - это строка, а значение - массив Json, считанный из файла. Мне нужно go через это для нескольких файлов, чтобы заполнить вывод. json. Пока что значение наполняется успешно.

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

Однако, когда я добавляю еще один --argjson, чтобы заполнить ключ ("TODO") желаемым значением $FQDN, он завершается ошибкой.

$ 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

Итак Моя цель - получить что-то вроде того, что показано ниже, но приведенное выше сообщение об ошибке недостаточно полезно. Любая помощь приветствуется.

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

1 Ответ

4 голосов
/ 18 июня 2020

В строке:

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

есть несколько ошибок:

  1. Фраза --argjson fqin="FQIN" неверна. За подробностями обращайтесь к руководству по jq. Здесь достаточно сказать, что желаемого эффекта можно добиться, написав --arg fqin "$FQIN".

  2. Выражение jq {$fqin: $cves} неверно. Когда имя ключа указывается с использованием переменной, переменная должна быть заключена в круглые скобки: {($fqin): $cves}. (Действительно, всякий раз, когда имя ключа указано косвенно, определяющее выражение должно быть заключено в круглые скобки.)

...