简体   繁体   English

jQuery为现有json添加大价值

[英]jq add large value to existing json

I have a large existing json object and I'd like to add a new key/value to the object where the value can be very large (many K). 我有一个现有的大型json对象,我想向该对象添加一个新的键/值,其中值可能非常大(很多K)。

Using jq I can do something like this: 使用jq我可以做这样的事情:

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

Is there a good way to replace big blob of text with a shell or jq variable? 有没有一种很好的方法用shell或jq变量替换big blob of text

The following is illustrative of what can be done if your version of jq supports "--argjson". 以下是您的jq版本支持“ --argjson”时可以做什么的说明。 Similar things can be done with --arg and --argfile . --arg--argfile可以完成类似的操作。

#!/bin/bash

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

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

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

Result: 结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM