JSON to Crystal Converter Online

Generate Crystal classes from any JSON payload with JSON::Serializable, typed properties, and nested classes. 100% in your browser.

What is a JSON to Crystal converter?

A JSON to Crystal converter reads a JSON sample and emits typed Crystal classes that include JSON::Serializable — a standard-library module that compiles in from_json and to_json methods at build time. The output is idiomatic Crystal that drops straight into a Lucky, Kemal, or Amber project.

Hand-writing Crystal classes from a long JSON payload is slow and easy to typo — and Crystal is strict, so a missing required property fails compilation rather than silently nilling. OpenFormatter walks the JSON tree once, generates idiomatic Crystal, and runs entirely in your browser so internal API responses never leave your machine.

How to generate Crystal from JSON — 4 steps

  1. Paste a representative JSON response. Type inference reads the first observed value of each field — booleans must be true/false, numbers must be unquoted.
  2. Click Convert. The tool emits a Root class plus one separate class per nested object, all including JSON::Serializable.
  3. Copy and paste into your shard. Each class is independent; split into separate .cr files under src/models.
  4. Deserialize. Call Root.from_json(string) — the method is generated automatically by JSON::Serializable.

JSON to Crystal type mapping

Sample JSON

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "tags": ["admin", "engineer"]
}

Generated Crystal

require "json"

class Root
  include JSON::Serializable

    property id : Int32
    property name : String
    property active : Bool
    property tags : Array(String)
end

JSON::Serializable

Generated classes include the stdlib module so from_json and to_json are wired up at compile time — no runtime reflection.

Nested classes

Every nested JSON object becomes its own Crystal class. Deserialization recurses naturally through the type system.

Client-Side Only

JSON is parsed in JavaScript inside the browser. Internal API responses, tokens, or PII never reach a server.

Common use cases

  • check_circleBuilding a Lucky framework REST controller request and response models
  • check_circleModelling JSON payloads in a Kemal microservice from an upstream Ruby or Node API
  • check_circleProducing Amber framework data classes from a public JSON spec
  • check_circleModelling Kafka or NATS event payloads that flow through a Crystal consumer
  • check_circleBuilding CLI tools that parse JSON output from kubectl, terraform, or aws-cli
  • check_circleGenerating typed config classes for shard.yml or app-specific config.json
  • check_circleModelling third-party webhook payloads (Stripe, GitHub) in a Crystal handler
  • check_circleProducing GraphQL response shapes when calling an external GraphQL API

Why client-side generation matters

The JSON you paste often contains real customer data, OAuth tokens, signed URLs, or internal field names you do not want indexed by a third-party converter. OpenFormatter generates the Crystal entirely in JavaScript on your device — open the Network tab in DevTools and you will see zero requests when you click Convert. Safe for enterprise use, safe behind a corporate proxy, safe when the API contract is itself confidential.

Need other compiled-language tooling?

Generate Rust, Go, or Ruby data classes — all browser-side.

Frequently Asked Questions

What is Crystal?

Crystal is a statically typed, compiled language with a syntax inspired by Ruby — readable and expressive, but with type inference, native binaries, and performance comparable to C. It ships with a JSON module in the standard library that handles parsing and serialization with first-class language support.

How does JSON::Serializable work?

JSON::Serializable is a Crystal module you include in a class. At compile time it inspects the property declarations and generates from_json and to_json methods that map each JSON key to its corresponding property. Combined with Crystal’s type system, type errors at deserialization become compile-time errors.

How are nullable fields handled?

Mark a property nullable with the union type syntax: property name : String? — equivalent to String | Nil. The generator emits non-nullable types based on the sample (since the field was present); add ? after the type if you know the field is sometimes missing or null.

How are nested JSON objects modelled?

Each nested object becomes its own Crystal class that also includes JSON::Serializable. The parent class declares the property as the nested class type; deserialization recurses automatically because the nested class also has from_json generated.

How are JSON arrays handled?

A JSON array becomes Array(T) where T is inferred from the first element. Arrays of strings become Array(String); arrays of objects become Array(ChildClass). For arrays of mixed types use Array(JSON::Any) after generation.

What about JSON.mapping?

JSON.mapping was the older macro-based approach (Crystal pre-0.32). Modern Crystal (0.32+) prefers JSON::Serializable as a module include — cleaner syntax, better composition with other modules, and identical runtime behavior. The generator emits JSON::Serializable; if you target a legacy codebase, swap to JSON.mapping by hand.

Is the JSON uploaded to your servers?

No. Conversion runs entirely in your browser via JavaScript. Open DevTools → Network and click Convert — no requests are made. Pasting JSON containing tokens or proprietary data never leaves your machine.

How do I deserialize the generated classes?

Call Root.from_json(json_string) — the class method is generated by JSON::Serializable. Serialize back with my_root.to_json. For nested classes the method recurses automatically. Errors are raised as JSON::SerializableError at compile time when types do not match the schema.

JSON to Crystal Converter Online — Serializable Class