JSON to Ruby Converter

Paste a JSON sample and get back idiomatic Ruby: one class per object with attr_accessor for each key and an initialize that assigns every field. Nested objects become their own classes named after the key, and arrays carry an inline comment naming the element type. Inferred Ruby types (String, Integer, Float, true/false) are noted next to each accessor so you can see the shape at a glance.

How to use the JSON to Ruby Converter

Paste a JSON object (or an array whose first element is an object) into the input. Set the Root class name — it defaults to Root but you'll usually want something domain-specific like User or Invoice. Click Generate. The output is a complete set of plain Ruby classes you can paste straight into a .rb file or a model directory.

Each class gets an attr_accessor line listing all of its keys, followed by an initialize that takes one keyword argument per key and assigns it to the matching instance variable. Nested objects are emitted as their own classes — a user key holding an object produces a User class, and the parent's accessor is annotated with that class name. Arrays get a comment naming the inferred element type, since Ruby arrays are untyped at the language level. Use Copy to grab the result, or Example to load a representative payload and see the structure the generator produces.

Generating Ruby classes from JSON

Ruby has no static type system and no built-in equivalent of a typed DTO, so developers reach for one of two patterns when they need a structured value object: a class exposing attr_accessor for each field, or a Struct.new(:a, :b, keyword_init: true). The accessor-class style is the more flexible of the two — it lets you add validation, computed methods, and references to other generated classes for nested data, which a flat Struct cannot express cleanly. That is why this generator emits a class with attr_accessor and a keyword initialize rather than a Struct.

Type inference here is deliberately lightweight, because Ruby will not enforce anything at runtime anyway. JSON strings map to String, booleans to true/false, and numbers split into Integer or Float depending on whether the sampled value has a fractional part — JSON itself draws no such distinction, so the split is a best guess from the data you paste. null becomes NilClass. These types appear only as trailing comments next to each accessor; they document intent without imposing constraints, which matches how Ruby code is normally read.

Nested objects are promoted to top-level classes named by capitalising the key (shipping_addressShippingAddress), and the generator walks the tree depth-first so deeply nested payloads still produce a flat, ordered list of classes. Arrays are annotated with the element type drawn from the first element, since an array of Order objects and an array of integers read very differently in practice. If you also work in statically typed languages, the JSON to TypeScript generator applies the same structural inference but emits enforceable interfaces.

Common use cases

  • Rails value objects — turn an external API payload into plain Ruby classes that wrap and document the response.
  • Service-object inputs — scaffold a keyword-argument class for a service or interactor from an example hash.
  • Webhook handlers — model an incoming webhook body as a class so the rest of your code reads event.user.email instead of nested hash fetches.
  • Test fixtures — generate a class around a JSON fixture so factories and specs share one documented shape.
  • Gem authoring — produce response wrapper classes for an API client gem without hand-writing every accessor.
  • Onboarding — paste an unfamiliar JSON blob to instantly see its nesting and field names as readable Ruby.

Frequently asked questions

Why classes with attr_accessor instead of Struct?

A class with attr_accessor supports nested classes, validation, and custom methods, which a flat Struct cannot express cleanly. The generator picks the accessor-class style so nested objects can each become their own referenced class.

How are nested objects and arrays handled?

Each nested object becomes its own class named by capitalising the key (for example billing becomes Billing). Arrays get a comment naming the inferred element type, since Ruby arrays are untyped at the language level.

Are the inferred types enforced at runtime?

No. Ruby has no static typing, so the inferred types (String, Integer, Float, true/false) appear only as comments next to each accessor to document the expected shape. They impose no runtime constraint.

How does it tell Integer from Float?

By the sampled value. A number with a fractional part is typed Float; a whole number is typed Integer. JSON has a single number type, so this is a best-effort guess from the example you paste.

Does any of this leave my browser?

No. Parsing and code generation run entirely client-side in JavaScript. Nothing you paste is uploaded anywhere.