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. Paste a JSON sample here and you get one plain Ruby class per object: 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. The output is checked with ruby -c and by running new and to_h on the input. It all runs in your browser.
How to use it
The classes update as you type.
Paste a JSON sample
Paste into the JSON Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}Set Class Name for the root class. Untick attr_accessor to get read-only attr_reader attributes. If the input won't parse, check it with the JSON Validator.
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
Copy the code or Download a snake_case .rb file, require_relative it, 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.
What if the JSON root is an array?
The objects in it become the root class, and a comment shows the map call that builds them. A root that is null, a string or a list of plain values gives a short message instead of code.
Is my JSON uploaded?
No. The classes are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.
Other JSON Tools You Might Need
Need the same data in another language, or want to validate it first?