Skip to content

Instantly share code, notes, and snippets.

@theirishpenguin
Created October 12, 2012 16:58
Show Gist options
  • Save theirishpenguin/3880239 to your computer and use it in GitHub Desktop.
Save theirishpenguin/3880239 to your computer and use it in GitHub Desktop.
Simple ruby script to take a json file and output it in a nicely indented format
#! /usr/bin/env ruby
require 'json'
unless ARGV.length == 1
puts "
Usage: prettify_json.rb path/to/file.json
It's simple! It is strongly recommended that you redirect any errors to an
error.txt file (to make them easy to read) as follows...
eg. prettify_json.rb path/to/input.json 2> error.txt
You can also redirect the prettified json output to a file too...
eg. prettify_json.rb path/to/input.json 2> error.txt > output.json
"
exit 1
end
input_filepath = ARGV.first
unless File.exist?(input_filepath)
puts "File '#{input_filepath}' not found."
exit 1
end
pretty_json = ''
text = File.read(input_filepath)
begin
json = JSON.parse(text)
pretty_json = JSON.pretty_generate(json)
rescue
$stderr.puts "#{$!}\n#{$!.backtrace.join("\n")}"
end
puts pretty_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment