Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Created October 26, 2013 22:51
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/7175517 to your computer and use it in GitHub Desktop.
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.
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