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

Ln 1, Col 1Empty

Ruby

Your result will appear here.
No result yet

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

  1. Paste a sample of your JSON, drop a .json file on the box, or load it from a link with URL.
  2. Pick a class name and a style.
  3. Click Generate Ruby, or press Ctrl + Enter.
  4. Copy the code, or download it as a .rb file.

Example

JSON
{
  "id": 7,
  "name": "Emily Clark",
  "admin": true,
  "address": { "city": "London" }
}
Ruby (excerpt)
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
end
Usage
require "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_accessorData.define
Ruby versionAny3.2 and newer
Change a valueuser.name = "Ana"Immutable; make a copy with user.with(name: "Ana")
EqualitySame object onlyEqual when all values are equal
Good forObjects you update, older appsAPI responses and value objects
Data.define style
Address = Data.define(:city, :zip) do
  def self.from_hash(hash)
    new(
      city: hash["city"],
      zip: hash["zip"],
    )
  end
end

How JSON Types Become Ruby

JSONRuby
StringString
Whole number / decimalInteger / Float
true / falsetrue / false (commented as Boolean)
ObjectA class, built with from_hash
ArrayArray<Item>; arrays of objects are mapped to their class
null, or a key missing in some itemsnil, 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 example Time.iso8601(hash["edited_at"]) after require "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.

Related Tools