JSON to Code

JSON to Ruby

Paste a JSON sample. Get Ruby types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with Ruby chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this Ruby:

require 'json'
require 'dry-types'
require 'dry-struct'

module Types
  include Dry.Types(default: :nominal)

  Integer = Strict::Integer
  Nil     = Strict::Nil
  Bool    = Strict::Bool
  Hash    = Strict::Hash
  String  = Strict::String
  Double  = Strict::Float | Strict::Integer
end

class Address < Dry::Struct
  attribute :city,     Types::String
  attribute :postcode, Types::Nil
end

class Root < Dry::Struct
  attribute :id,        Types::Integer
  attribute :root_name, Types::String
  attribute :active,    Types::Bool
  attribute :joined,    Types::String
  attribute :tags,      Types.Array(Types::String)
  attribute :address,   Address
  attribute :scores,    Types.Array(Types::Double)
end

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

# This code may look unusually verbose for Ruby (and it is), but
# it performs some subtle and complex validation of JSON data.
#
# To parse this JSON, add 'dry-struct' and 'dry-types' gems, then do:
#
#   root = Root.from_json! "{…}"
#   puts root.scores.first
#
# If from_json! succeeds, the value returned matches the schema.

require 'json'
require 'dry-types'
require 'dry-struct'

module Types
  include Dry.Types(default: :nominal)

  Integer = Strict::Integer
  Nil     = Strict::Nil
  Bool    = Strict::Bool
  Hash    = Strict::Hash
  String  = Strict::String
  Double  = Strict::Float | Strict::Integer
end

class Address < Dry::Struct
  attribute :city,     Types::String
  attribute :postcode, Types::Nil

  def self.from_dynamic!(d)
    d = Types::Hash[d]
    new(
      city:     d.fetch("city"),
      postcode: d.fetch("postcode"),
    )
  end

  def self.from_json!(json)
    from_dynamic!(JSON.parse(json))
  end

  def to_dynamic
    {
      "city"     => city,
      "postcode" => postcode,

and 51 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. `json` is in the standard library.

The file is long for Ruby, and on purpose: it checks every field as it reads, rather than trusting the hash.

Use it at the edge of the program, where data arrives from outside.

The trap to know about

A symbol key and a string key are different in Ruby. The reader uses strings, which is what `JSON.parse` gives by default.

The options you can change

These belong to Ruby, so the row of switches changes with the language.

OptionWhat it doesDefault
Types onlyPlain types onlyfalse
Type strictnessType strictnessstrict | coercible | none

Questions

How do I turn JSON into Ruby?

Paste your JSON into the box, choose Ruby, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my Ruby project?

Nothing. `json` is in the standard library.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to JavaScript JSON to Flow JSON to PHP All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code