是否有(Unix)shell脚本将JSON格式化为可读格式?
基本上,我希望它转换以下内容:
{ "foo": "lorem", "bar": "ipsum" }
…变成这样:
{
"foo": "lorem",
"bar": "ipsum"
}
是否有(Unix)shell脚本将JSON格式化为可读格式?
基本上,我希望它转换以下内容:
{ "foo": "lorem", "bar": "ipsum" }
…变成这样:
{
"foo": "lorem",
"bar": "ipsum"
}
当前回答
这里有一个比Json的pretify命令更好的Ruby解决方案。gem colorful_json相当不错。
gem install colorful_json
echo '{"foo": "lorem", "bar": "ipsum"}' | cjson
{
"foo": "lorem",
"bar": "ipsum"
}
其他回答
你也可以选择使用在线工具。
我发现http://jsonprettyprint.net是最简单和最简单的。
对于Node.js,您也可以使用“util”模块。它使用语法高亮显示、智能缩进、从键中删除引号,并使输出尽可能漂亮。
cat file.json | node -e "process.stdin.pipe(new require('stream').Writable({write: chunk => {console.log(require('util').inspect(JSON.parse(chunk), {depth: null, colors: true}))}}))"
PHP版本,如果您有PHP>=5.4。
alias prettify_json=php -E '$o = json_decode($argn); print json_encode($o, JSON_PRETTY_PRINT);'
echo '{"a":1,"b":2}' | prettify_json
$ sudo apt-get install edit-json
$ prettify_json myfile.json
如果你安装了Node.js,你可以用一行代码自己创建一个Node.js。创建一个漂亮的文件:
>vim漂亮
#!/usr/bin/env node
console.log(JSON.stringify(JSON.parse(process.argv[2]), null, 2));
添加执行权限:
>chmod+x漂亮> ./漂亮的“{”foo“:”lorem“,”bar“:”ipsum“}”
或者如果JSON在文件中:
#!/usr/bin/env node
console.log(JSON.stringify(require("./" + process.argv[2]), null, 2));
> ./漂亮的文件.json