Transforms XML into JSON
Add this to your Gemfile
gem'xml2json',git: 'git@github.com: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>andrea@wearemonk.com</email></author> <author><name>Giuseppe</name><email>giuseppe@wearemonk.com</email></author> </root>'XML2JSON.parse(xml)
output is a string
{"root":{"authors":[{"name":"Andrea", "email":"andrea@wearemonk.com"},{"name":"Giuseppe", "email":"giuseppe@wearemonk.com"}]}}XML2JSON.parse_to_hash(xml)
output is a hash
{"root"=>{"authors"=>[{"name"=>"Andrea","email"=>"andrea@wearemonk.com"},{"name"=>"Giuseppe","email"=>"giuseppe@wearemonk.com"}]}}