JSON to Ruby Struct

Paste JSON and generate clean Ruby Structs with nested structs and a recursive from_h builder — ready for any Ruby or Rails project.

Input JSON

Generated Ruby

Your generated Ruby structs will appear here.

About JSON to Ruby Struct

This free tool converts any JSON object or array into clean, idiomatic Ruby Structs in one click. It inspects your JSON, builds a struct for every object it finds, and wires up nested structs and arrays of structs so the shape of your data is captured in real Ruby code you can paste straight into a Ruby script, a Sinatra app, or a Rails model.

Because Ruby is dynamically typed, the generated members are not annotated with types. Instead, the value is in the structure: JSON keys in camelCase become snake_case members, nested objects become their own Structs, and the optional from_h class method recursively hydrates the whole tree — calling NestedStruct.from_h for inner objects and mapping over arrays of objects automatically.

Choose the output that fits your Ruby version and style: a classic Struct with keyword_init: true, an immutable Data.define value object (Ruby 3.2+), or a plain class with attr_accessor and a keyword-argument constructor. Everything runs entirely in your browser, so your JSON never leaves your device.

How to Use JSON to Ruby Struct

  1. Paste a JSON object or array into the input box, or click Load Sample to try it.
  2. Set the root struct name for the top-level object.
  3. Choose an output style: Struct, Data.define, or a plain class.
  4. Pick whether from_h reads string keys (h["id"]) or symbol keys (h[:id]), and toggle the from_h builder on or off.
  5. Copy the generated Ruby and paste it into your project, then build objects with YourStruct.from_h(JSON.parse(json)).
Use JSON.parse(json) for string keys, or JSON.parse(json, symbolize_names: true) if you selected symbol keys. Data.define requires Ruby 3.2 or newer.

What it handles

  • ✅ Nested structs
  • ✅ Arrays of structs via map
  • ✅ Recursive from_h hydration
  • Struct, Data & plain class
  • ✅ String or symbol hash keys
  • camelCasesnake_case

FAQ

Does this tool send my JSON to a server?

No. All parsing and code generation happens in your browser, so your JSON stays on your device.

Why are there no type annotations?

Ruby is dynamically typed, so Structs and classes don't declare member types. The tool captures the data's shape instead — nested structs, arrays of structs, and a from_h builder that rebuilds the whole object graph.

What is the difference between Struct and Data.define?

Struct creates a mutable value class and is available in every Ruby version. Data.define (Ruby 3.2+) creates an immutable value object with read-only attributes. Both are constructed with keyword arguments here.

How do I build objects from a JSON string?

Parse the JSON and pass the hash to from_h: User.from_h(JSON.parse(json)). If you chose symbol keys, parse with JSON.parse(json, symbolize_names: true).

How are JSON keys like createdAt handled?

They become snake_case members (created_at), the idiomatic Ruby convention, while from_h still reads the original JSON key so parsing works correctly.

Does it support root-level JSON arrays?

Yes. A root array of objects generates a struct for the element shape plus a comment showing how to map the array: JSON.parse(json).map { |e| Item.from_h(e) }.