Project

xml2json

0.01
No commit activity in last 3 years
No release in over 3 years
Turn XML into JSON
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
~> 10.1
~> 2.14

Runtime

 Project Readme

xml2json

Code Climate Gem Version Dependency Status Build Status

Transforms XML into JSON

Installation

Add this to your Gemfile

gem 'xml2json', git: 'git@github.com:monksoftware/xml2json.git'

Configuration

Attributes, text and namespaces key name can be customized, defaults are _attributes, _text and _namespaces

XML2JSON.config do |c|
	c.attributes_key = "attr"
	c.namespaces_key = "nsp"
	c.text_key = "txt"
end

Usage

XML2JSON.parse(xml_string)         # => this outputs a json string
XML2JSON.parse_to_hash(xml_string) # => this outputs a ruby hash

Examples

Input XML

xml = '<?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>'

Parse to JSON string

XML2JSON.parse(xml)

output is a string

{"root":{"authors":[{"name":"Andrea", "email":"andrea@wearemonk.com"},{"name":"Giuseppe", "email":"giuseppe@wearemonk.com"}]}}

Parse to Ruby Hash

XML2JSON.parse_to_hash(xml)

output is a hash

{"root" => {"authors" => [{"name" => "Andrea", "email" => "andrea@wearemonk.com"},{"name" => "Giuseppe", "email" => "giuseppe@wearemonk.com"}]}}