如何通过终端缩进多个JSON文件

时间:2018-05-09 13:30:05

标签: python json bash indentation

是否有一种快速方法可以通过终端在当前目录下缩进给定的一组JSON文件,类似于使用" Pretty JSON"或"缩进JSON" Sublime文本的插件?

是通过shell脚本还是python脚本等?

1 个答案:

答案 0 :(得分:3)

有python:

 echo '{"foo": "bar"}' | python -m json.tool 

jq

echo '{"foo": "bar"}' | jq .

现在替换目录中的每个JSON文件:

 ls *.json | xargs -I % sh -c "cat % | python -m json.tool > %.new ; mv %.new %"