Skip to content

Instantly share code, notes, and snippets.

@timuruski
Last active November 10, 2016 11:02
Show Gist options
  • Save timuruski/d417db605880bd79631c to your computer and use it in GitHub Desktop.
Save timuruski/d417db605880bd79631c to your computer and use it in GitHub Desktop.
A dumb way to prettify JSON on the command line.

pjson

Prettifies JSON on the command line.

Installation

Copy into anywhere on $PATH and chmod +x the file. Remove the .rb if you like.

Usage

Prettify a file:

$ pjson minified.json

Works with multiple files, thanks to the magic of ARGF.

$ pjson *.json

Plays nice with pipes:

$ pjson minified.json | less

Plays nice with curl, etc.:

$ curl http://example.org/users.json | pjson
#!/usr/bin/env ruby
require 'json'
begin
puts JSON.pretty_generate JSON.load(ARGF)
rescue JSON::ParserError => error
# Clean up error messages like: 123: unexpected token at '...'
puts error.message
.sub(/\A\d+: unexpected token at '/, '')
.sub(/'\Z/, '')
warn "--- Not parseable JSON."
exit(1)
rescue Errno::EPIPE
exit(74)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment