Skip to content

Instantly share code, notes, and snippets.

@itamar
Created July 28, 2013 08:00
Show Gist options
  • Select an option

  • Save itamar/6097882 to your computer and use it in GitHub Desktop.

Select an option

Save itamar/6097882 to your computer and use it in GitHub Desktop.
Convert XML to JSON
require 'json'
Hash.from_xml('<variable type="product_code">5</variable>').to_json # => "{\"variable\":\"5\"}"
require 'net/http'
s = Net::HTTP.get_response(URI.parse('http://stackoverflow.com/feeds/tag/ruby/')).body
Hash.from_xml(s).to_json
# for pretty output
JSON.pretty_generate(Hash.from_xml('<variable type="product_code">5</variable>'))
# http://stackoverflow.com/questions/1530324/ruby-xml-to-json-converter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment