Created
October 26, 2013 22:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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