Created
October 26, 2013 22:51
-
-
Save EronHennessey/7175517 to your computer and use it in GitHub Desktop.
Converts JSON to XML. Can use either filenames passed as arguments, or operates on standard input. Uses the 'xmlsimple' library for XML output.
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 'xmlsimple' | |
json_file, xml_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) | |
xml_text = XmlSimple.xml_out(data) | |
unless xml_file.nil? | |
f = File.new(xml_file, 'w+') | |
f.write(xml_text) | |
f.close | |
else | |
$stdout.puts xml_text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment