JSON to Ruby Class Generator
Paste JSON and get Ruby classes with from_hash, to_h and to_json, nested classes for nested objects and type comments for every attribute. Choose plain classes or Data.define (Ruby 3.2+). Runs in your browser, so your data is never uploaded.
JSON Input
Ruby
What Does This Generator Do?
Ruby's JSON.parse gives you plain hashes, so a typo like post["tittle"] silently returns nil. This tool writes real Ruby classes from an example of your JSON: one class per object, an attribute for every key, a from_hash method that also builds the nested classes, and to_h / to_json to turn them back into the same JSON.
How to Convert JSON to Ruby
- Paste a sample of your JSON, drop a .json file on the box, or load it from a link with URL.
- Pick a class name and a style.
- Click Generate Ruby, or press Ctrl + Enter.
- Copy the code, or download it as a .rb file.
Example
{
"id": 7,
"name": "Emily Clark",
"admin": true,
"address": { "city": "London" }
}class User
attr_accessor :id # Integer
attr_accessor :name # String
attr_accessor :admin # Boolean
attr_accessor :address # Address
def self.from_hash(hash)
new(
id: hash["id"],
name: hash["name"],
admin: hash["admin"],
address: hash["address"]&.then { |h| Address.from_hash(h) },
)
end
def to_h
{
"id" => @id,
"name" => @name,
"admin" => @admin,
"address" => @address&.to_h,
}
end
endrequire "json" user = User.from_hash(JSON.parse(text)) user.address.city # "London" user.to_json # back to the same JSON
The full output also has initialize with keyword arguments, to_json and the Address class. We ran the generated code in Ruby 3.4 and checked that from_hash followed by to_json gives back the original data, in both styles.
Class or Data.define?
| Class with attr_accessor | Data.define | |
|---|---|---|
| Ruby version | Any | 3.2 and newer |
| Change a value | user.name = "Ana" | Immutable; make a copy with user.with(name: "Ana") |
| Equality | Same object only | Equal when all values are equal |
| Good for | Objects you update, older apps | API responses and value objects |
Address = Data.define(:city, :zip) do
def self.from_hash(hash)
new(
city: hash["city"],
zip: hash["zip"],
)
end
endHow JSON Types Become Ruby
| JSON | Ruby |
|---|---|
| String | String |
| Whole number / decimal | Integer / Float |
true / false | true / false (commented as Boolean) |
| Object | A class, built with from_hash |
| Array | Array<Item>; arrays of objects are mapped to their class |
null, or a key missing in some items | nil, commented as Type, nil |
Keys are turned into snake_case attributes (firstName → first_name). Keys that are Ruby keywords or core method names get a suffix, so class becomes class_value and hash becomes hash_value, while to_h still writes the original JSON key.
Tips
- Use a full sample. Keys missing from your example cannot appear in the class. Include an item that has every optional field.
- Dates stay strings. JSON has no date type. Convert in
from_hash, for exampleTime.iso8601(hash["edited_at"])afterrequire "time". - Need validation? Describe the shape with the JSON Schema Generator and check data with a JSON Schema gem.
- Broken JSON? Trailing commas and single quotes are repaired automatically. For anything else, use JSON Repair.
- Reading and writing JSON in other languages: see the JSON Cheat Sheet.
Frequently Asked Questions
How do I convert JSON to a Ruby class?
Paste a sample of your JSON and click Generate Ruby. You get one class per object with attributes, a from_hash method that also builds nested classes, and to_h and to_json to write the same JSON back.
Should I use a class or Data.define?
Data.define (Ruby 3.2+) makes immutable value objects that are equal when their values are equal, which suits API responses. Use a class with attr_accessor when you need to change values or support older Ruby.
What happens to keys like class or firstName?
Keys become snake_case attributes, so firstName is first_name. Keys that clash with Ruby keywords or core methods get a suffix, like class_value, and to_h still writes the original key.
Is my JSON uploaded?
No. The classes are generated in your browser and your data never leaves your device.