JSON to Ruby Class
Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up, no sample data kept.
How to generate a Ruby class from JSON
- Paste your JSON. Paste one representative JSON object into the box above.
- Generate the class. Click Generate Ruby class. The tool emits a class with attr_accessor for the root and each nested object.
- Parse and assign. require 'json', call JSON.parse on your string, and assign the values onto an instance through the accessors.
How the mapping works
Ruby is dynamically typed, so the generated class does not annotate types. Each JSON object becomes a class with attr_accessor for every key, and every nested object gets its own class so you can model the whole shape. Keys that are not valid Ruby identifiers are sanitised.
Arrays and nested objects
An array of objects produces a class for the element type; arrays of scalars need no class. Nested objects each get a named class. Populate the accessors yourself after parsing with JSON.parse, or feed the hash into a constructor you add.
Using the result
Parse the JSON with require 'json'; data = JSON.parse(str, symbolize_names: true), then assign the values onto an instance via the accessors.
FAQ
Why no type annotations?
Ruby does not declare field types. The generated class exposes each field through attr_accessor; you assign whatever value JSON.parse returns.
How do I populate the object?
Parse with JSON.parse to get a Hash, create an instance, and assign each accessor — or add an initialize that takes a hash and sets instance variables.
What about nested objects?
Each nested JSON object becomes its own class so the structure is explicit. Build the inner objects first, then assign them to the outer accessor.