如何使用jq向现有json文件添加对象
我有一个空的output.json,我想用{key,value}对填充它,其中key是字符串,value是从文件读取的json数组。我需要检查多个文件以填充output.json。到目前为止,该值已成功填充如何使用jq向现有json文件添加对象,json,jq,Json,Jq,我有一个空的output.json,我想用{key,value}对填充它,其中key是字符串,value是从文件读取的json数组。我需要检查多个文件以填充output.json。到目前为止,该值已成功填充 $ jq --argjson cves "$(cat my-scan-result-N.json)" '.+={"TODO": $cves}' output.json { "TODO": [ { "cvePK": "CVE-2020-11656", "su
$ 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以使用所需值$FQDN填充键TODO时,它会失败并出现错误
$ 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": ""
}
]
}
该行:
有几个错误:
短语-argjson fqin=fqin不正确。有关详细信息,请参阅jq手册。这里只需说明,您可以通过在$fqin中写入-arg fqin来实现所需的效果
jq表达式{$fqin:$cves}不正确。使用变量指定键名时,该变量必须用括号括起来:{$fqin:$cves}。实际上,无论何时间接指定键名,指定表达式都必须用括号括起来
该行:
有几个错误:
短语-argjson fqin=fqin不正确。有关详细信息,请参阅jq手册。这里只需说明,您可以通过在$fqin中写入-arg fqin来实现所需的效果
jq表达式{$fqin:$cves}不正确。使用变量指定键名时,该变量必须用括号括起来:{$fqin:$cves}。实际上,无论何时间接指定键名,指定表达式都必须用括号括起来
这太棒了。1.是的,语法是错误的。我忽略了语法。2. ${fqin}:这一直是罪魁祸首。非常感谢你的帮助,@peak!这太棒了。1.是的,语法是错误的。我忽略了语法。2. ${fqin}:这一直是罪魁祸首。非常感谢你的帮助,@peak!
jq --argjson cves "$(cat my-scan-result.json)" --argjson fqin="FQIN" '.+={$fqin: $cves}' output.json