Skip to content
XUtilHub

JSON to Ruby Class & Struct Generator

Convert JSON to Ruby classes with keyword arguments, from_hash and to_h, or to compact Structs. Snake_case attributes and nested objects handled.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Ruby

Generate Ruby classes with keyword-argument constructors, a from_hash builder and to_h, or compact Struct definitions, all with snake_case attribute names.

Key features

  • Ruby classes with keyword-argument constructors
  • from_hash builders that construct nested objects
  • to_h methods that restore the original JSON keys
  • Or compact Struct definitions (keyword_init)
  • snake_case attributes; Ruby keywords made safe
  • Optional values default to nil

Ruby objects from JSON

Ruby makes it easy to dig through JSON.parse hashes, but hash["customer"]["address"]["city"] gets fragile fast. Generate plain Ruby classes with readable attributes, a from_hash builder and to_h.

Example

{ "id": 1001, "price": 9.5, "tags": ["gift"], "customer": { "name": "Ada" }, "coupon": null }
class Order
  attr_accessor :id, :price, :tags, :customer, :coupon

  def initialize(id:, price:, tags:, customer:, coupon: nil)
    @id = id
    @price = price
    @tags = tags
    @customer = customer
    @coupon = coupon
  end

  def self.from_hash(h)
    new(
      id: h['id'],
      price: h['price'],
      tags: h['tags'],
      customer: Customer.from_hash(h['customer']),
      coupon: h['coupon'],
    )
  end

  def to_h
    { 'id' => @id, 'price' => @price, 'tags' => @tags, 'customer' => @customer&.to_h, 'coupon' => @coupon }
  end
end
order = Order.from_hash(JSON.parse(response.body))
order.customer.name # => "Ada"

Struct mode

Order = Struct.new(:id, :price, :tags, :customer, :coupon, keyword_init: true)

Struct mode is ideal for flat data: Order.new(**JSON.parse(json, symbolize_names: true)). The keys must match the member names, and nested hashes stay hashes.

Tips

  • Ruby 3.2+: consider Data.define(...) for immutable value objects.
  • Type checking: add RBS or Sorbet signatures using the attribute types shown in your JSON.
  • Serialization: JSON.generate(order.to_h) turns objects back into JSON.

Generating for another dynamic language? See JSON to Python or JSON to Elixir.

How to use the JSON to Ruby

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Ruby appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Class or Struct?

Structs are compact and great for simple data. Classes add from_hash for nested objects and to_h for converting back, which is useful for API clients.

How do I build an object from JSON?

Parse the JSON and call from_hash: order = Order.from_hash(JSON.parse(json)).

Does it work with Rails?

Yes. Use the classes as plain Ruby objects for API responses in services or jobs. For database tables, use ActiveRecord models instead.

What happens to keys like 'class' or 'end'?

Attributes named after Ruby keywords get a _value suffix (class_value), while from_hash and to_h keep the original key.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools