[英]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.我有一个空的 output.json 并且我想用 {key, value} 对填充它,其中键是字符串,值是 Json 数组从文件中读取。 I need to go through this for multiple files to populate the output.json.
我需要通过此 go 来填充多个文件以填充 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.但是,当我添加另一个
--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
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": ""
}
]
}
The line:该行:
jq --argjson cves "$(cat my-scan-result.json)" --argjson fqin="FQIN" '.+={$fqin: $cves}' output.json
has several errors:有几个错误:
The phrase --argjson fqin="FQIN"
is incorrect.短语
--argjson fqin="FQIN"
不正确。 Please see the jq manual for details.有关详细信息,请参阅 jq 手册。 Suffice it to say here that you could achieve the desired effect by writing
--arg fqin "$FQIN"
.在这里只想说,您可以通过编写
--arg fqin "$FQIN"
来达到预期的效果。
The jq expression {$fqin: $cves}
is incorrect. jq 表达式
{$fqin: $cves}
不正确。 When a key name is specified using a variable, the variable must be enclosed in parentheses: {($fqin): $cves}
.当使用变量指定键名时,变量必须用括号括起来:
{($fqin): $cves}
。 (Indeed, whenever the key name is specified indirectly, the specifying expression must be enclosed in parentheses.) (实际上,无论何时间接指定键名,指定表达式都必须用括号括起来。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.