jq 为现有 json 添加大的价值

jq add large value to existing json

我有一个很大的现有 json 对象,我想向该对象添加一个新的 key/value,其中的值可能非常大(很多 K)。

使用 jq 我可以做这样的事情:

echo $item | jq '. + {readme:"big blob of text"}'

有什么好的方法可以用shell或jq变量替换big blob of text吗?

以下说明了如果您的 jq 版本支持“--argjson”,可以做什么。 --arg--argfile.

也可以做类似的事情
#!/bin/bash

function bigblob {
cat <<EOF 
big blob of text
EOF
}

item='{"a": "a"}'

bigblob | jq -R --argjson item "$item" '$item + {"readme": .}'

结果:

{
  "a": "a",
  "readme": "big blob of text"
}