JSON to Ruby Converter
Plain Ruby classes from a JSON sample, nested objects included
JSON Input
Ruby Output
What does the JSON to Ruby converter generate?
JSON.parse gives you nested hashes, which is fine until you're three levels deep typing order["customer"]["email"] and hoping you spelled the keys right. Hand it one sample document and it writes a plain Ruby class for every object inside: snake_case attributes declared with attr_accessor, an initialize(hash = {}) that reads the original keys, and a to_h that turns the object back into a hash with those same keys.
Nested objects become their own classes and are built inside initialize, so RootObject.new(JSON.parse(json)) gives you root.subscriber.account.carrier all the way down. Arrays of objects are mapped element by element (value.map { |x0| Device.new(x0) }), and a nil nested value stays nil instead of raising. to_h does the reverse, so JSON.generate(root.to_h) gives you JSON again.
Attribute names are snake_case (subscriberId becomes subscriber_id). Names that are Ruby keywords, or that would override an Object method you rely on, like class, hash or send, get a _value suffix. Class names that would reopen a core class, such as Data or Set, get a Model suffix. These are plain Ruby objects, not ActiveRecord models or Structs. We syntax-checked the generated code with ruby -c and round-tripped real payloads through new and to_h. The page itself never runs Ruby or uploads your JSON; it writes the classes as text.
How to use it
The classes update as you type.
Paste a JSON sample
Paste into JSON Input, read a .json or .txt from disk with Upload, or click Sample. A blog post with an embedded author is a typical Rails API payload:
{"postId":42,"title":"Upgrading to Rails 8","author":{"name":"Cordelia Hart"},"tags":["rails","upgrade"]}Class Name names the root class; with Post you get attr_accessor :post_id, :title, :author, :tags and a separate Author class that initialize builds for you. Untick attr_accessor for read-only attr_reader attributes. Seeing an Invalid JSON message instead of classes? The JSON Validator marks the line.
Read the classes
The Ruby Output panel shows a usage comment, then the classes with the nested ones first, so the file loads top to bottom.
Copy or download
Hit Copy, or Download to save it under a snake_case .rb name. Then require_relative that file and call RootObject.new(JSON.parse(json_string)).
Common Questions
Are these ActiveRecord models?
No. They are plain Ruby classes with no database behind them. Use them for API payloads, service objects or tests. For Rails models you still write a migration and inherit from ApplicationRecord; the attribute list here can help with that.
Why not Struct or Data?
Struct and Data don't know how to turn a nested hash into nested objects. The generated initialize does, and to_h writes the original JSON keys back. You can still swap in Struct if your JSON is flat.
What happens to keys like 2fa or first name?
They become valid attribute names (n_2fa, first_name), and initialize and to_h keep using the original keys, so nothing is lost on the way in or out.
Does it check types?
No. Ruby is dynamic, so values are stored as JSON.parse returns them. Add your own validation if the input isn't trusted.
Can I paste an array of posts instead of a single object?
Yes. The objects become the root class, and the header comment shows how to build all of them at once: JSON.parse(json_string).map { |row| Post.new(row) }. An array without objects, such as [true, false], has no keys to turn into attributes, so the page prints a Nothing to generate note instead.
Is my JSON uploaded?
No. Your JSON is turned into Ruby inside the browser and isn't sent anywhere. Input stops at 256 KB, and the page tells you so rather than hanging; trim the array to a few representative records and paste again.
Other JSON Tools You Might Need
A Rails API usually has a JavaScript client on the other end, so JSON to TypeScript is a natural next stop. The Schema Generator gives you a contract to check incoming payloads against before they reach initialize.