Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby XML to JSON Converter?

Is there a library to convert XML to JSON in Ruby?

like image 223
Lance Avatar asked Oct 07 '09 08:10

Lance


1 Answers

A simple trick:

First you need to gem install json, then when using Rails you can do:

require 'json' require 'active_support/core_ext' Hash.from_xml('<variable type="product_code">5</variable>').to_json #=> "{\"variable\":\"5\"}" 

If you are not using Rails, then you can gem install activesupport, require it and things should work smoothly.

Example:

require 'json' require 'net/http' require 'active_support/core_ext/hash' s = Net::HTTP.get_response(URI.parse('https://stackoverflow.com/feeds/tag/ruby/')).body puts Hash.from_xml(s).to_json 
like image 81
khelll Avatar answered Nov 04 '22 16:11

khelll