Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Created October 26, 2013 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EronHennessey/7175501 to your computer and use it in GitHub Desktop.
Save EronHennessey/7175501 to your computer and use it in GitHub Desktop.
Converts JSON to YAML. Works either with filenames passed in as arguments, or by piping standard input.
require 'json'
require 'yaml'
json_file, yaml_file = ARGV
json_text = nil
unless json_file.nil?
if File.exist?(json_file)
json_text = File.new(json_file, 'r').read
else
puts "File #{json_file} does not exist!"
exit
end
else
json_text = $stdin.read
end
data = JSON.parse(json_text)
yaml_text = data.to_yaml
unless yaml_file.nil?
f = File.new(yaml_file, 'w+')
f.write(yaml_text)
f.close
else
$stdout.puts yaml_text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment