YAML to Ruby Converter
Generate Ruby types from YAML instantly. Optional fields are detected automatically. Runs entirely in your browser.
Input
Output
Ready
Drop a file to load it
About this tool
Paste a YAML sample and get Ruby type definitions you can use immediately. Emits plain classes with `attr_accessor` and a `from_json` constructor.
When you would use it
- Wrapping an API response in a value object
- Bootstrapping models from a real YAML response instead of writing them by hand
- Keeping types in step with an API that has changed
Worked example
id: 4815 name: Ada Lovelace active: true score: 91.5 roles: - admin - editor address: city: London postcode: NW1
# frozen_string_literal: true
class Root
attr_accessor :id
attr_accessor :name
attr_accessor :active
attr_accessor :score
attr_accessor :roles
attr_accessor :address
def initialize(id: nil, name: nil, active: nil, score: nil, roles: nil, address: nil)
@id = id
@name = name
@active = active
@score = score
@roles = roles
@address = address
end
def self.from_json(h)
new(id: h['id'], name: h['name'], active: h['active'], score: h['score'], roles: h['roles'], address: h['address'])
end
end
class Address
attr_accessor :city
attr_accessor :postcode
def initialize(city: nil, postcode: nil)
@city = city
@postcode = postcode
end
def self.from_json(h)
new(city: h['city'], postcode: h['postcode'])
end
end
Things worth knowing
- Every object in an array is merged when inferring the shape, so a field missing from one item is marked optional rather than assumed to always be present.
- Identical nested shapes are de-duplicated into a single type instead of generating near-duplicates.
- Array element types are named from the singular of the field name, so
postsproduces aPosttype. - YAML resolves unquoted scalars to types, so
nobecomes a boolean and1.10becomes a number. Quote values you want treated as text.
Questions
Frequently asked
How are optional fields detected?
By comparing every object in an array. A key present in some records but not others becomes optional; a key present in all of them is required. This is why passing several records gives better results than one.
Can it handle deeply nested YAML?
Yes. Nested objects become separate named types, referenced from their parent, at any depth.
Is my data uploaded anywhere?
No. Every tool on JSONWorks runs entirely in your browser using JavaScript. Your input is never sent to a server, never logged and never stored. The simplest proof is to disconnect from the internet — the tools keep working. The site does load Google Fonts and a Google Analytics page-view tag, so you will see those two requests in your network tab, but neither receives anything you type or paste.
Related