Ruby XML to JSON Converter?

46,433

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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
Share:
46,433
Lance
Author by

Lance

I read JavaScript and am attempting to emit Assembly from it.

Updated on May 26, 2021

Comments

  • Lance
    Lance almost 3 years

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