63

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Lance
  • 75,200
  • 93
  • 289
  • 503

7 Answers7

111

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
Smar
  • 8,109
  • 3
  • 36
  • 48
khelll
  • 23,590
  • 15
  • 91
  • 109
  • 11
    Sometimes it is important to keep the XML attributes as well. For example, in your example above, "product_code" was lost. What library or libraries would you suggest to keep the attributes? – David J. Aug 17 '10 at 17:34
  • 12
    An update, in case if your running a ruby script for the same as a standalone program to avoid undefined method "from_xml". You need to include require ‘active_support/core_ext’ , as given here:- http://masonoise.wordpress.com/2010/01/07/hash-from_xml/#comments . This works fine in Rails otherwise.. – boddhisattva Apr 20 '12 at 12:49
  • 3
    Small improvement to have pretty-printed JSON: `JSON.pretty_generate(Hash.from_xml('5'))` – Tom De Leu Jul 09 '13 at 15:28
  • 3
    Note you need to `require 'json'` and `require 'active_support/core_ext'` to get Tom De Leu's example to work – Ashley Feb 20 '15 at 13:01
  • 2
    Note you need to require 'json' and 'active_support/core_ext/hash' - NOT 'active_support/core_ext' – etayluz May 29 '15 at 15:06
  • You also need to convert from string to Hash: eval(Hash.from_xml(s).to_json) – etayluz May 29 '15 at 15:17
26

I'd use Crack, a simple XML and JSON parser.

require "rubygems"
require "crack"
require "json"

myXML  = Crack::XML.parse(File.read("my.xml"))
myJSON = myXML.to_json
neilfws
  • 32,751
  • 5
  • 50
  • 63
13

If you wish to keep all attributes I recommend cobravsmongoose http://cobravsmongoose.rubyforge.org/ which uses the badgerfish convention.

<alice sid="4"><bob sid="1">charlie</bob><bob sid="2">david</bob></alice>

becomes:

{"alice":{"@sid":"4","bob":[{"$":"charlie","@sid":"1"},{"$":"david","@sid":"2"}]}}

code:

require 'rubygems'
require 'cobravsmongoose'
require 'json'
xml = '<alice sid="4"><bob sid="1">charlie</bob><bob sid="2">david</bob></alice>'
puts CobraVsMongoose.xml_to_hash(xml).to_json
Corban Brook
  • 21,270
  • 4
  • 28
  • 34
5

You may find the xml-to-json gem useful. It maintains attributes, processing instruction and DTD statements.

Install

gem install 'xml-to-json'

Usage

require 'xml/to/json'
xml = Nokogiri::XML '<root some-attr="hello">ayy lmao</root>'
puts JSON.pretty_generate(xml.root) # Use `xml` instead of `xml.root` for information about the document, like DTD and stuff

Produces:

{
  "type": "element",
  "name": "root",
  "attributes": [
    {
      "type": "attribute",
      "name": "some-attr",
      "content": "hello",
      "line": 1
    }
  ],
  "line": 1,
  "children": [
    {
      "type": "text",
      "content": "ayy lmao",
      "line": 1
    }
  ]
}

It's a simple derivative of xml-to-hash.

Community
  • 1
  • 1
Maarten
  • 6,894
  • 7
  • 55
  • 90
4

If you're looking for speed I would recommend Ox since it's pretty much the fastest option from the ones already mentioned.

I ran some benchmarks using an XML file that has 1.1 MB from omg.org/spec and these are the results(in seconds):

xml = File.read('path_to_file')
Ox.parse(xml).to_json                    --> @real=44.400012533
Crack::XML.parse(xml).to_json            --> @real=65.595127166
CobraVsMongoose.xml_to_hash(xml).to_json --> @real=112.003612029
Hash.from_xml(xml).to_json               --> @real=442.474890548
Dan F.
  • 345
  • 1
  • 3
  • 12
2

Assuming you're using libxml, you can try a variation of this (disclaimer, this works for my limited use case, it may need tweaking to be fully generic)

require 'xml/libxml'

def jasonized
  jsonDoc = xml_to_hash(@doc.root)
  render :json => jsonDoc
end

def xml_to_hash(xml)
  hashed = Hash.new
  nodes = Array.new

  hashed[xml.name+"_attributes"] = xml.attributes.to_h if xml.attributes?
  xml.each_element { |n| 
    h = xml_to_hash(n)
    if h.length > 0 then 
      nodes << h 
    else
      hashed[n.name] = n.content
    end
  }
  hashed[xml.name] = nodes if nodes.length > 0
  return hashed
end
erwin
  • 31
  • 2
  • 2
    home-made code solutions are generally a bad idea for generic problems - except for learning purposes. Especially when the question is specifically an enquiry about an existing library. – Titou Nov 04 '14 at 15:14
2

Simple but with heavy dependencies (active_support); it's not much of a problem if you already are in a Rails environment.

require 'json'
require 'active_support/core_ext'

Hash.from_xml(xml_string).to_json

Else it would be probably better to use ox or rexml to have much lighter dependencies and more performance.

noraj
  • 3,964
  • 1
  • 30
  • 38
Alex V
  • 18,176
  • 5
  • 36
  • 35
  • It's actually khelll anwser and it requires `active_support` which is a pretty big external dependency to use `from_xml`. `json` is also required for `to_json` even if it's stdlib so we can say native. – noraj May 26 '21 at 08:31