Convert JSON to Ruby hash or class online. Paste any JSON object and instantly see the equivalent Ruby data structure and parsing code. Free, no signup required.
Open JSON to Ruby Converter →Ruby ships with a built-in JSON library that makes reading and writing JSON data effortless. Whether you are building a Rails API, processing webhook payloads, or consuming third-party REST services, understanding how Ruby handles JSON is an essential skill for any Ruby developer.
The JSON module is part of Ruby's standard library, so no additional gem installation is required for most use cases. You simply add require 'json' at the top of your file and you are ready to parse and generate JSON.
When you parse a JSON object in Ruby, it becomes a Hash with string keys. JSON arrays become Ruby Arrays, JSON strings map to Ruby String, numbers map to Integer or Float, booleans map to true or false, and JSON null becomes Ruby nil.
require 'json'
json_string = '{"name":"Alice","age":30,"active":true}'
data = JSON.parse(json_string)
puts data["name"] # => "Alice"
puts data["age"] # => 30
puts data["active"] # => true
puts data.class # => Hash
Ruby provides two methods for deserializing JSON: JSON.parse and JSON.load. They look similar but have very different safety profiles, and choosing the wrong one can introduce serious security vulnerabilities.
json_class key. This is a known security risk with untrusted input. Only use with fully trusted data sources.require 'json'
# Safe - always prefer this for external input
data = JSON.parse('{"user":"alice","score":99}')
# Symbol keys option - convenient for internal use
data = JSON.parse('{"user":"alice"}', symbolize_names: true)
puts data[:user] # => "alice"
# Generating JSON from a Ruby hash
hash = { name: "Bob", roles: ["admin", "editor"] }
puts hash.to_json
# => {"name":"Bob","roles":["admin","editor"]}
# Pretty printed output
puts JSON.pretty_generate(hash)
Plain hashes work well for simple cases, but for complex JSON data it is often cleaner to map the parsed data into a Struct or OpenStruct. This gives you dot-notation access to fields and makes your code more readable and self-documenting.
Struct is faster and more memory-efficient. You define the fields explicitly and get a proper Ruby class with a named structure.
require 'json'
# Define a Struct for your JSON shape
User = Struct.new(:name, :email, :age, keyword_init: true)
json = '{"name":"Carol","email":"carol@example.com","age":28}'
data = JSON.parse(json, symbolize_names: true)
user = User.new(**data)
puts user.name # => "Carol"
puts user.email # => "carol@example.com"
puts user.age # => 28
OpenStruct is more flexible - it creates attributes dynamically without a predefined structure. It is slower than Struct but useful for prototyping or handling JSON shapes that vary at runtime.
require 'json'
require 'ostruct'
json = '{"product":"Widget","price":9.99,"in_stock":true}'
obj = JSON.parse(json, object_class: OpenStruct)
puts obj.product # => "Widget"
puts obj.price # => 9.99
puts obj.in_stock # => true
Here are the patterns Ruby developers use most frequently when working with JSON in real applications:
require 'json'
data = JSON.parse(File.read('config.json'))
puts data["database"]["host"]
require 'json'
config = { host: "localhost", port: 5432, ssl: true }
File.write('config.json', JSON.pretty_generate(config))
require 'json'
json = '{"user":{"name":"Dave","address":{"city":"London"}}}'
data = JSON.parse(json)
city = data.dig("user", "address", "city")
puts city # => "London"
require 'json'
json = '[{"id":1,"name":"Alpha"},{"id":2,"name":"Beta"}]'
items = JSON.parse(json)
items.each { |item| puts "#{item['id']}: #{item['name']}" }
# => 1: Alpha
# => 2: Beta
Require the built-in JSON library with require 'json', then call JSON.parse(your_json_string). The result is a Ruby Hash for JSON objects and a Ruby Array for JSON arrays. All nested structures are parsed automatically.
JSON.parse is safe and should be used for all external or user-supplied data. It only creates standard Ruby types. JSON.load can deserialize arbitrary Ruby objects and carries a security risk when used with untrusted input.
Pass symbolize_names: true as an option: JSON.parse(str, symbolize_names: true). This lets you access values with hash[:key] notation instead of hash["key"].
Yes. Call .to_json on any Hash or Array after requiring the JSON library. For human-readable output, use JSON.pretty_generate(hash), which adds indentation and line breaks.
Free, instant, 100% private. No account needed.
Open JSON to Ruby Converter →Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Rust | JSON to SQL