Skip to content

Instantly share code, notes, and snippets.

@thbar
Created April 18, 2012 09:43
Show Gist options
  • Save thbar/2412396 to your computer and use it in GitHub Desktop.
Save thbar/2412396 to your computer and use it in GitHub Desktop.
Ruby JSON pipe pretty-printer

A quick ruby command line to pretty-print piped json.

Usage

Put this somewhere in your path, with chmod +x, then:

curl http://myapp.com/stuff.json | json

#!/usr/bin/env ruby
require 'rubygems' # 1.8.7
require 'bundler'
Bundler.setup
require 'json'
if STDIN.tty?
puts 'your_command | json [--no-color]'
else
data = JSON.parse(STDIN.read)
if ARGV.include?('--no-color')
puts JSON.pretty_generate(data)
else
require 'ap'
ap data
end
end
@jmervine
Copy link

You can also simply add the following to bashrc or zshrc

function json {
    ruby -rjson -e 'puts JSON.pretty_generate(JSON.parse(STDIN.read))'
}

Doesn't give you the color or the help, but works well enough.

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