Convert between XML, Hash, YAML, JSON in Ruby - Conversion Cheat Sheet
Ruby, Uncategorized Add commentsHere’s a little XML/JSON/YAML/Hash conversion cheat sheet for Ruby:
First, let’s create an XML document:
require ‘rubygems’
require ‘nokogiri’
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {xml.products {xml.widget {xml.id_ “10″xml.name “Awesome widget”}}}endmy_xml = builder.to_xml
XML To Hash:
require ‘active_support’ #if you have Rails installed
my_hash = Hash.from_xml(my_xml)
Without Rails/ActiveSupport, have a look at Crack which very fast and will usually give you enough fields. If you have attributes and a text value in the same node however (<person age=”10″>joe</person), you will only get the value back, not the attribute. Update (again): For text nodes (any node that contains a string), crack will return an attributes hash in addition to the text content.
my_hash = Crack::XML.parse(my_xml)
Hash To Object?
Have a look here: http://blog.jayfields.com/2008/01/ruby-hashtomod.html
Hash To JSON:
require ‘json’
my_json = my_hash.to_json
JSON back to Hash:
my_hash = JSON.parse(my_json)
Also have a look at Crack:
my_hash = Crack::JSON.parse(my_json)
Hash To YAML:
my_yaml = my_hash.to_yaml
YAML back to Hash:
my_hash = YAML::load(my_yaml)
Bonus Points – Hash to XML:
require ‘xmlsimple’
my_xml = XmlSimple.xml_out(my_hash, {’KeepRoot’ => true})
There is currently no way to preserve the attributes (like <person age=”10″>Joe</person>) with such conversion from Hash to XML.
June 11th, 2011 at 5:57 pm
just FYI, Crack doesn’t eat the attributes it sticks them in a hidden pouch. They can be retrieved using the .attributes method.
June 12th, 2011 at 3:37 pm
@Joe: Thanks, very helpful info! I’ve updated the post.
August 17th, 2011 at 8:30 pm
Hmm, I just rechecked and can’t find the .attributes method. As of version 0.8.1 crack can either return the XML-element’s text value or the attribute, not both; see here: http://railstips.org/blog/archives/2009/04/01/crack-the-easiest-way-to-parse-xml-and-json/
September 27th, 2011 at 10:09 am
Dirk,
Have a look at the 0.3.1 source in file ‘xml.rb’ lines 83-92. The attributes method only gets added to text nodes. It could probably be hacked to add them everywhere but you’d need to watch out for side effects.
October 10th, 2011 at 1:22 am
@Joe:
You’re right, I must have tried on a different type of node or I simply overlooked it. Sorry about that.
October 21st, 2011 at 4:50 pm
It sure would be nice if xml, json, and yaml string conversions to and from ruby objects were all of the same form. I guess the xml stuff would be difficult because there is not a standard way of encoding an object in xml, but at least json (.from_json as well as .to_json) and yaml (.from_json as well as .to_json) would be nice. Perhaps I will do that…