Filipe Freire
Filipe Freire

Reputation: 833

How to indent multiple JSON files via terminal

Is there a quick way to indent a given set of JSON files under a current directory via terminal, similarly to using "Pretty JSON" or "Indent JSON" plugins on Sublime text?

Either by a shell script or python script, etc. ?

Upvotes: 0

Views: 352

Answers (1)

Jean Carlo Machado
Jean Carlo Machado

Reputation: 1618

Having python:

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

Or jq:

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

Now replacing every JSON file in the directory:

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

Upvotes: 4

Related Questions