JSON to Ruby Class Converter
Turn a JSON sample into a Ruby class in your browser. Optional and nullable fields are kept apart, and nothing is uploaded.
What the JSON to Ruby generator does
Each object becomes a class with attr_accessor for every key and a keyword-argument initializer, which is the shape a Rails developer expects to see. Ruby is dynamically typed, so no type annotations are invented: every argument defaults to nil and the caller passes what it has. Key names are snake_cased, which is the Ruby convention for methods and instance variables.
How to use it
JSON to Ruby Class Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Ruby class below.
Worked example
Input:
{
"id": 42,
"displayName": "Ada Lovelace",
"active": true,
"score": 9.5,
"tags": [
"math",
"engine"
],
"address": {
"city": "London",
"country": "GB"
},
"projects": [
{
"name": "Analytical Engine",
"year": 1843
},
{
"name": "Note G",
"year": 1843,
"notes": "first published algorithm"
}
],
"nickname": null
}
Output:
# frozen_string_literal: true
# Generated from a JSON sample in the browser by AllTools — nothing was uploaded.
# Rename and adjust to taste; the shape is inferred from one example, not a schema.
class Root
attr_accessor :id, :display_name, :active, :score, :tags, :address, :projects, :nickname
def initialize(id: nil, display_name: nil, active: nil, score: nil, tags: nil, address: nil, projects: nil, nickname: nil)
@id = id
@display_name = display_name
@active = active
@score = score
@tags = tags
@address = address
@projects = projects
@nickname = nickname
end
end
class Address
attr_accessor :city, :country
def initialize(city: nil, country: nil)
@city = city
@country = country
end
end
class Project
attr_accessor :name, :year, :notes
def initialize(name: nil, year: nil, notes: nil)
@name = name
@year = year
@notes = notes
end
end
Why it runs in your browser
The conversion happens on this page with JavaScript. Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere. The page reads JSON and returns Ruby class; reloading the page discards both.
Each object becomes a class with attr_accessor for every key and a keyword-argument initializer, which is the shape a Rails developer expects to see. Ruby is dynamically typed, so no type annotations are invented: every argument defaults to nil and the caller passes what it has. Key names are snake_cased, which is the Ruby convention for methods and instance variables.
Turn a JSON sample into a Ruby class in your browser. Optional and nullable fields are kept apart, and nothing is uploaded.
How to use it
JSON to Ruby Class Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Ruby class below.
Why it runs in your browser
The conversion happens on this page with JavaScript. Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere. The page reads JSON and returns Ruby class; reloading the page discards both.
What to Expect
Best for
- Converting JSON to Ruby class when the data is already on screen.
What this tool keeps
- Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere.
Sample input
{ "id": 42, "displayName": "Ada Lovelace", "active": true, "score": 9.5, "tags": [ "math", "engine" ], "address": { "city": "London", "country": "GB" }, "projects": [ { "name": "Analytical Engine", "year": 1843 }, { "name": "Note G", "year": 1843, "notes": "first published algorithm" } ], "nickname": null }
Sample output
# frozen_string_literal: true # Generated from a JSON sample in the browser by AllTools — nothing was uploaded. # Rename and adjust to taste; the shape is inferred from one example, not a schema. class Root attr_accessor :id, :display_name, :active, :score, :tags, :address, :projects, :nickname def initialize(id: nil, display_name: nil, active: nil, score: nil, tags: nil, address: nil, projects: nil, nickname: nil) @id = id @display_name = display_name @active = active @score = score @tags = tags @address = address @projects = projects @nickname = nickname end end class Address attr_accessor :city, :country def initialize(city: nil, country: nil) @city = city @country = country end end class Project attr_accessor :name, :year, :notes def initialize(name: nil, year: nil, notes: nil) @name = name @year = year @notes = notes end end
Frequently asked questions
How do I use the json to ruby class converter?
JSON to Ruby Class Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Ruby class below.
Is my input uploaded?
No. The text is parsed on this page with JavaScript and is never posted to a server.
Is there a size limit?
Yes: 5 MB per run. Larger input is refused with a message rather than freezing the tab.
Build custom tools for your store
Join the SDK program to create useful ecommerce utilities for your store.