Advertisement

JSON to Ruby Struct

Turn JSON into Ruby Struct definitions with keyword init. Nested structs, symbol members, and sensible defaults inferred from your sample.

JSON input

Paste JSON, optionally name the root type, then generate.

Your Ruby Struct code will appear here.
Advertisement

Lightweight models, Ruby style

Ruby favors duck typing over declarations, and Struct is the idiomatic middle ground between a hash and a full class. The generator creates one Struct per object level with keyword_init for readable construction.

From API hash to real objects

Raw parsed JSON gives you nested hashes with string keys. Structs wrap them in named attributes, so user.address.city replaces user['address']['city'] and typos become NoMethodError instead of silent nils.

JSON to Ruby Struct FAQs

What is a Ruby Struct?
Struct.new creates a class with named attributes, giving you readers, equality, and iteration for free. It is Ruby's lightest way to model structured data.
What does keyword_init do?
With keyword_init: true you construct instances like User.new(name: 'Ada'), which is clearer than positional arguments, especially for wide API objects.
How are nested objects handled?
Each nested object becomes its own Struct constant, so Address = Struct.new(:city, ...) is referenced by the parent, mirroring the JSON tree.
Does Ruby need type annotations?
No, Ruby is dynamically typed, so the Struct only declares member names. The value types are documented in comments inferred from the sample.
How do I build one from parsed JSON?
Parse with JSON.parse, then pass the hash with symbolized keys: User.new(**JSON.parse(text, symbolize_names: true)).
Advertisement