Skip to content

Instantly share code, notes, and snippets.

@jperras
Created November 22, 2011 20:36
Show Gist options
  • Save jperras/1386859 to your computer and use it in GitHub Desktop.
Save jperras/1386859 to your computer and use it in GitHub Desktop.
Mass prettification of JSON files.
for f in `find . -path "*.json"`; do python -mjson.tool $f $f.output && mv $f.output $f; done
@jperras
Copy link
Author

jperras commented Nov 22, 2011

Sadly, python -mjson.tool seems to leave trailing whitespace on each reformatted line. Had to go in and clean that up with find . -path "*.json" -print0 | xargs -0 sed -i '' -E "s/[[:space:]]*$//".

@jperras
Copy link
Author

jperras commented Nov 22, 2011

Additionally, I had originally created find . -path "*.json" -print0 | xargs -0 -I FILENAME python -mjson.tool FILENAME FILENAME, which would have worked fine. However, json.tool really doesn't like writing to the same file handle that it has opened for reading, and I didn't feel like rewriting it to be a bit smarter.

If anyone can see a way to do it with find and xargs (like I had originally intended) instead of going the bash script route, let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment