Transforms XML into JSON
Add this to your Gemfile
gem'xml2json',git: '[email protected]:monksoftware/xml2json.git'Attributes, text and namespaces key name can be customized, defaults are _attributes, _text and _namespaces
XML2JSON.configdo |c| c.attributes_key="attr"c.namespaces_key="nsp"c.text_key="txt"endXML2JSON.parse(xml_string)# => this outputs a json stringXML2JSON.parse_to_hash(xml_string)# => this outputs a ruby hashxml='<?xml version="1.0" encoding="utf-8"?> <root> <author><name>Andrea</name><email>[email protected]</email></author> <author><name>Giuseppe</name><email>[email protected]</email></author> </root>'XML2JSON.parse(xml) output is a string
{"root":{"authors":[{"name":"Andrea", "email":"[email protected]"},{"name":"Giuseppe", "email":"[email protected]"}]}}XML2JSON.parse_to_hash(xml) output is a hash
{"root"=>{"authors"=>[{"name"=>"Andrea","email"=>"[email protected]"},{"name"=>"Giuseppe","email"=>"[email protected]"}]}}