XML to JSON Converter for Ruby
Free online xml to json converter with Ruby code examples
Working with xml to json converter in Ruby? Our free online xml to json converter helps Ruby developers format, validate, and process data instantly. Below you will find Ruby code examples using nokogiri / active_support so you can achieve the same result programmatically in your own projects.
Try the XML to JSON Converter Online
Use our free XML to JSON directly in your browser — no setup required.
Open XML to JSONRuby Code Example
require 'nokogiri'
require 'json'
xml = '<root><user><name>Alice</name><age>30</age></user></root>'
doc = Nokogiri::XML(xml)
def xml_to_hash(node)
if node.element_children.empty?
return node.content
end
result = {}
node.element_children.each do |child|
result[child.name] = xml_to_hash(child)
end
result
end
hash = xml_to_hash(doc.root)
puts JSON.pretty_generate(hash)Quick Setup
Library: nokogiri / active_support
gem install nokogiriRuby Tips & Best Practices
- Nokogiri is the standard XML/HTML parser for Ruby
- Use Hash.from_xml from ActiveSupport for simple conversions
- Nokogiri supports XPath and CSS selectors for querying