What is JSON to Ruby Class Generator?
Ruby is a dynamic, object-oriented language where everything is an object, and its elegant syntax makes it a popular choice for web development, scripting, and API design. This generator transforms your JSON data into idiomatic Ruby class definitions with full serialization support. It offers four distinct output modes tailored to different Ruby ecosystems: Plain Ruby classes with attr_accessor and manual from_json/to_json methods, Ruby Struct for lightweight value objects, ActiveModel classes with validations and serialization for Rails applications, and Dry-Struct classes for immutable, type-safe value objects using the dry-rb ecosystem. Each mode generates complete, ready-to-use code including constructors, serialization methods, and proper type annotations inferred from your JSON values.
How to Use
- Enter a root class name in the input field — this becomes the name of the top-level Ruby class
- Select a framework mode: Plain Ruby, Struct, ActiveModel, or Dry-Struct depending on your project needs
- Toggle Symbolize Keys to use Ruby symbols instead of string keys in JSON parsing
- Toggle Require JSON to include the require "json" header in the output
- Paste your JSON data into the input area and click "Generate Ruby Code"
- Copy the generated classes into your Ruby project and customize as needed
Why Use This Tool?
Tips & Best Practices
- Provide JSON with representative values — the Ruby type for each field is inferred from actual data, so null fields default to NilClass
- ActiveModel mode adds presence validations for every non-nil field, which is useful for form objects and API payloads in Rails
- Dry-Struct mode requires the dry-struct gem and produces immutable objects — ideal for value objects and configuration data that should not change after creation
- Enable Symbolize Keys when your application uses symbol-based hash access (e.g., data[:name]) instead of string-based access (e.g., data["name"])
- The Require JSON toggle adds the standard library import — disable it if your project already loads JSON through Bundler or another require mechanism
Frequently Asked Questions
How are JSON types mapped to Ruby types?
JSON strings map to String, integers map to Integer, decimal numbers map to Float, booleans map to TrueClass, null maps to NilClass (marked as nullable), arrays map to Array, and nested objects become separate Ruby classes. In Dry-Struct mode, the Types module is used: Types::String, Types::Integer, Types::Float, Types::Bool, Types::Nil, and nullable fields use Types::Nil | Types::String.
When should I NOT use this generator?
Skip this tool if you need custom Ruby logic like computed attributes, callbacks, or associations that go beyond simple data containers. Also, if your JSON uses polymorphic structures (e.g., a field that is sometimes an object and sometimes an array), the generated code will not handle that correctly without manual modifications.
What is ActiveModel mode?
ActiveModel mode generates classes that include ActiveModel::Serialization and ActiveModel::Validations. It provides attr_accessor for all fields, an initialize method that accepts keyword arguments, and presence validations for non-nil fields. The serializable_hash method handles serialization, making it compatible with Rails controllers and Jbuilder templates.
What is Dry-Struct mode?
Dry-Struct mode generates classes that inherit from Dry::Struct with attributes defined using the Types module (e.g., attribute :name, Types::String). Dry-Struct provides immutable, type-safe objects with built-in validation and coercion at instantiation time. It requires the dry-struct gem and is ideal for applications that value functional programming patterns.
How are nested objects handled?
Nested JSON objects are converted to separate Ruby classes with their own definitions. They are ordered children-first so that referenced classes are defined before the parent class. The parent class calls the nested class from_json_hash and to_hash methods for recursive serialization.
Is my data sent to any external server?
No. All processing happens entirely inside your browser. Your JSON data never leaves your device, and no network requests are made during the conversion.
Real-world Examples
Generating an ActiveModel user class for a Rails API
Create a validated ActiveModel class from a JSON payload for use in a Rails API controller.
{
"name": "Alice",
"email": "alice@example.com",
"age": 30,
"role": "admin"
}class User
include ActiveModel::Serialization
include ActiveModel::Validations
attr_accessor :name, :email, :age, :role
validates :name, presence: true
validates :email, presence: true
validates :age, presence: true
validates :role, presence: true
def initialize(name:, email:, age:, role:)
@name = name
@email = email
@age = age
@role = role
end
...
endDry-Struct value objects for a configuration model
Generate immutable Dry-Struct classes from a nested JSON configuration object.
{
"database": {
"host": "localhost",
"port": 5432,
"name": "myapp_production"
},
"cache": {
"enabled": true,
"ttl": 3600
}
}class Database < Dry::Struct attribute :host, Types::String attribute :port, Types::Integer attribute :name, Types::String ... end class Cache < Dry::Struct attribute :enabled, Types::Bool attribute :ttl, Types::Integer ... end class Root < Dry::Struct attribute :database, Database attribute :cache, Cache ... end