JSON to Ruby Class Generator

JSON Input
1
Root Class
Ruby Output

Last updated:

Jsonic's JSON to Ruby Class generator converts a JSON object into Ruby class definitions. Each class gets attr_accessor declarations for all fields, an initialize(hash) constructor that reads values from a hash, a self.from_json(json_str) class method for parsing a JSON string directly, a to_h instance method that returns a plain Ruby hash, and a to_json instance method compatible with Ruby's standard JSON library. JSON strings map to String, integers to Integer, decimals to Float, booleans to TrueClass or FalseClass, null to nil, and arrays to Array. Nested JSON objects generate separate named classes declared above the class that references them. camelCase JSON keys are converted to snake_case for Ruby field names. All processing runs in your browser with no data upload.

How to convert JSON to Ruby classes

  1. Paste your JSON object into the left panel, or click Example to load a sample.
  2. Optionally change the root class name (default: Root).
  3. Click Generate.
  4. Add require 'json' at the top of your Ruby file (the generator includes it automatically).
  5. Use Root.new(hash) to instantiate from a hash, or Root.from_json(json_str) to parse directly from a JSON string.

FAQ

What Ruby version is required?

The generated classes use attr_accessor and the standard json library, both available since Ruby 2.0. No gems are required — just add require 'json' at the top of your file, which the generator includes automatically. The safe navigation operator (&.) used for nested object serialization requires Ruby 2.3 or newer.

How are JSON types mapped to Ruby types?

JSON strings map to String, JSON integers map to Integer, JSON decimals map to Float, JSON booleans map to TrueClass or FalseClass (Ruby has no single boolean type), JSON null maps to nil, JSON arrays map to Array, and JSON objects generate a named Ruby class. The type comments are included as documentation in the attr_accessor block.

How are nested JSON objects handled?

Each nested JSON object generates a separate named Ruby class. Nested classes are declared above the class that references them so they are defined before use. The parent class initializes nested fields with NestedClass.new(hash['key']) and uses the safe navigation operator (&.to_h) when serializing back to a hash, so nil nested values are handled safely.

Can I use the generated classes with Rails?

Yes. Rails includes the json gem by default, so require 'json' is already satisfied in a Rails context. Copy the generated class files into your app/models or lib directory. You can instantiate from a parsed request body with Root.new(params.to_h), or deserialize a raw JSON string from an external API with Root.from_json(response.body). The to_json method makes serializing back to JSON straightforward.

How is camelCase handled in Ruby field names?

The generator converts camelCase JSON keys to snake_case Ruby attribute names following Ruby conventions — for example, userId becomes user_id and firstName becomes first_name. The hash accessor in initialize still uses the original JSON key string (hash['userId']) so parsing works correctly without any additional configuration.

What if my JSON is an array at the root level?

Paste a single element from the array to generate the class definition, then parse the full array with: data = JSON.parse(json_str); items = data.map { |h| Root.new(h) }. Alternatively, wrap the array in a container object such as { "items": [...] } before pasting it into the tool to generate a wrapper class with an array field.