是否有(Unix)shell 脚本以人类可读的形式格式化 JSON?
基本上,我希望它改变以下内容:
{ "foo": "lorem", "bar": "ipsum" }... 进入这样的事情:
{
    "foo": "lorem",
    "bar": "ipsum"
}使用 Python 2.6+,你可以做到:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool或者,如果 JSON 在文件中,您可以:
python -m json.tool my_json.json如果 JSON 来自互联网源,例如 API,您可以使用
curl http://my_url/ | python -m json.tool为了方便所有这些情况,您可以创建一个别名:
alias prettyjson='python -m json.tool'为了更方便,更多的打字准备就绪:
prettyjson_s() {
    echo "$1" | python -m json.tool
}
prettyjson_f() {
    python -m json.tool "$1"
}
prettyjson_w() {
    curl "$1" | python -m json.tool
}对于所有上述情况。你可以将它放在.bashrc ,它每次都可以在 shell 中使用。像prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'一样调用它。 
我使用JSON.stringify的 “space” 参数在 JavaScript 中漂亮地打印 JSON。 
例子:
// Indent with 4 spaces
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, 4);
// Indent with tabs
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, '\t');从带有 nodejs 的 Unix 命令行,在命令行上指定 json:
$ node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));" \
  '{"foo":"lorem","bar":"ipsum"}'返回:
{
    "foo": "lorem",
    "bar": "ipsum"
}从带有 Node.js 的 Unix 命令行,指定包含 JSON 的文件名,并使用四个空格的缩进:
$ node -e "console.log(JSON.stringify(JSON.parse(require('fs') \
      .readFileSync(process.argv[1])), null, 4));"  filename.json使用管道:
echo '{"foo": "lorem", "bar": "ipsum"}' | node -e \
"\
 s=process.openStdin();\
 d=[];\
 s.on('data',function(c){\
   d.push(c);\
 });\
 s.on('end',function(){\
   console.log(JSON.stringify(JSON.parse(d.join('')),null,2));\
 });\
"