python - 是否可以在 CLI 中修改 json 对象?

标签 python json linux bash command-line-interface

我有数千个包含 JSON 对象的文件 比如

{ "mykey" :"myvalue"}

我必须在每个文件中添加一个键值对 例如。

{ "mykey" :"myvalue", "newkey": "newvalue"}

我知道我可以轻松地编写 Python 脚本来完成它。在 CLI 中有更简单的方法吗?有点像

   addjson "newkey" "newvalue" myfile*.json

最佳答案

您可以使用 json node.js 模块。 获取 node.js 后,您可以使用 sudo npm install -g json

安装它

用法 json -I -f myfile.json -e 'this.newkey="newvalue"'

同时只有一个文件但是正如你所说,你可以使用python 或者在 bash 中使用 for 循环的例子:for i in $(ls myfile*.json);做 json -I -f $i -e 'this.newkey="newvalue"';完成

文档:http://trentm.com/json/

关于python - 是否可以在 CLI 中修改 json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157257/

相关文章:

python - django-allauth:电子邮件确认

javascript - 将 NodeJS express 对象传递给 AngularJS 1.6

C编程中的命令注入(inject)

python - PyUserInput - 停止 PyMouseEvent

python - 如何检查哪个更大 : a**b or b**a for big numbers?

python - Plotly:如何消除x轴上的空白

java - JSON API Android解析教程

android - Android - Ruby on Rails - MySQL

linux - 将 plink 剂量 (.raw) 格式转换为 ped 格式

c -/usr/include/linux 和/usr/include/x86_64-linux-gnu/有什么区别