如何使用shell脚本将json对象添加到json文件 [英] how to add json object to json file using shell script

查看:890
本文介绍了如何使用shell脚本将json对象添加到json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json文件如下:

{"name" :"sam",
"age":23,
"designation":"doctor"}

现在我想使用bash脚本在文件末尾添加另一个json对象{"location":"canada"} 我已经试过echo"{" location:" canada}" >> sample.json

now i want to add another json object {"location":"canada"} at the end of the file using bash script i have tried echo "{"location":"canada"}">>sample.json

但结果

{"name" :"sam",
"age":23,
"designation":"doctor"} {location:canada}

但我希望它像这样

{"name" :"sam",
"age":23,
"designation":"doctor", 
"location":"canada"}

请建议我

推荐答案

sed -i '$s/}/,\n"location":"canada"}/' sample.json

结果:

{"name" :"sam",
"age":23,
"designation":"doctor",
"location":"canada"}

这篇关于如何使用shell脚本将json对象添加到json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆