CalcCafe

JSON to Ruby

Paste JSON and instantly get quicktype-style Ruby classes with inferred field types.

Example

Given this JSON:

{"id":1,"user":{"name":"Sam","vip":true},"tags":["a","b"]}

You get these Ruby classes:

class Root
 attr_accessor :id, :user, :tags

 # @param attrs [Hash]
 def initialize(attrs = {})
  @id = attrs[:id] # Integer
  @user = attrs[:user] # User
  @tags = attrs[:tags] # Array[String]
 end
end

class User
 attr_accessor :name, :vip

 # @param attrs [Hash]
 def initialize(attrs = {})
  @name = attrs[:name] # String
  @vip = attrs[:vip] # bool
 end
end

How it works

Parses your JSON, walks the structure to infer each field's type, and emits one Ruby class per distinct object shape (root named Root) with attr_accessor and a Hash initializer. Nested objects become their own classes and arrays are annotated with their element type.

Good to know

JSON to Ruby turns a sample JSON object into a set of plain Ruby classes, one per distinct object shape, each with attr_accessor declarations and a Hash-based initializer. It's aimed at Ruby and Rails developers who are wiring up a client for a third-party API, modeling a webhook payload, or just want a typed-looking starting point instead of passing raw hashes around. Paste a representative response, click Convert, and you get scaffolding you can drop into a file and refine.

Reach for it whenever you have an example payload but no schema. The root object always becomes a class named Root, every nested object becomes its own class named after the key it appeared under (converted to PascalCase), and arrays are labeled with their element type. The type information lives in trailing comments like # Integer or # Array[String] rather than in real code, so the output documents intent without imposing a runtime type system Ruby doesn't have.

Read the result top to bottom: each class block lists its fields, and the comment after each assignment tells you what the converter inferred from your sample. Because inference is based purely on the example you paste, the quality of the output depends on how complete that example is. A few things worth knowing before you trust the result:

One practical caveat: the generated initializer just copies values out of the hash, so nested objects and array elements stay as raw hashes — it does not recursively instantiate the child classes for you. Treat the output as a typed map of your data rather than a finished deserializer, and add the nested object construction yourself if you need real objects all the way down. Everything runs locally in your browser, so it's safe to paste production payloads.

Frequently asked questions

How are nested objects and arrays of objects handled?
Each nested object becomes its own Ruby class named after its key (PascalCase). For an array of objects, the tool merges the keys across all elements into a single class and annotates the field as Array[ClassName] so optional keys are still captured.
What Ruby types does it infer for values?
Whole numbers map to Integer, decimals to Float, true/false to bool, strings to String, and null to nil. Each field gets a trailing comment with its inferred type, and arrays show their element type like Array[String].
Is my data uploaded anywhere?
No — it runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.

People also ask

How do I convert a JSON API response into Ruby classes?
Paste a representative JSON object into the input box and click Convert. The tool generates one Ruby class per object shape, with attr_accessor declarations, a Hash initializer, and inline comments showing each field's inferred type.
Does the generated Ruby code use real type checking?
No. Ruby is dynamically typed, so the inferred types appear only as trailing comments (for example # Integer or # Array[String]). The code itself just assigns values from the hash without enforcing any type.
Why are my JSON keys renamed in the Ruby output?
The converter normalizes keys to snake_case, which is Ruby's conventional style. A key like firstName becomes first_name, and the initializer reads it as attrs[:first_name] using a symbol key rather than the original string.
What happens when a JSON value is null or an array is empty?
A null value is inferred as nil and an empty array falls back to Object, because there is no value to infer a type from. Provide a sample with non-null, populated values to get accurate type annotations.
Can it handle arrays of objects with different keys?
Yes. It merges the keys from every element in the array into a single class and annotates the field as Array[ClassName], so fields that appear in only some elements are still included.
Does the initializer build nested objects automatically?
No. The generated initializer copies values straight from the hash, so nested objects and array items remain plain hashes. You would need to add code yourself to instantiate the nested classes recursively.
Is it safe to paste private or production JSON into this tool?
Yes. The tool runs entirely in your browser with no server upload, and it works offline once the page has loaded, so your data never leaves your device.
How does it decide between Integer and Float for numbers?
Whole numbers are inferred as Integer and numbers with a decimal point as Float. Because this is based on the sample you paste, a value like 4.0 written as 4 would be inferred as Integer.

Related tools