我知道如何回显/添加文本到文件末尾:
echo "{ "fruit":"apple" , "amount":"10" }" >> file.txt
我的问题是如何将对象添加到下面的 json 文件中:
文件 - file.txt(空):
{
"fruit": [
]
}
预期结果:
{
"fruit": [
{ "fruit":"apple" , "amount":"10" } #object to add
]
}
最佳答案
ed
是标准的文本编辑器。
#!/bin/bash
{
ed -s file.json <<EOF
/\"fruit\": \[
a
{ "fruit":"apple" , "amount":"10" } #object to add
.
wq
EOF
} &> /dev/null
虽然不知道为什么要为此使用 bash,但周围有更好的工具!
完成。
关于linux - Bash:将对象添加到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334238/