monksoftware / xml2json

Transform XML into JSON

Home Page:https://github.com/monksoftware/xml2json

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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"}]}}

About

Transform XML into JSON

https://github.com/monksoftware/xml2json


Languages

Language:Ruby 100.0%