What is JSON to Gleam Type Generator?
Gleam is a type-safe, functional programming language for the Erlang VM and JavaScript runtimes that brings strong static typing to the BEAM ecosystem. Custom types in Gleam are algebraic data types with named fields, similar to structs in Rust or records in Elm. This generator converts your JSON data into Gleam custom type definitions with proper type inference — strings become String, integers become Int, floats become Float, booleans become Bool, and null values become Option(T) when the option toggle is enabled. The generated code follows Gleam conventions with pub visibility, snake_case field names, and PascalCase type names.
How to Use
- Set the module name in the options bar (e.g., my_app/types) to match your Gleam project structure
- Toggle "Use Option for nullable fields" to wrap null values with Gleam's Option type instead of Nil
- Paste your JSON object into the input area — the tool requires a JSON object, not an array or primitive
- Click "Generate" to produce Gleam custom type definitions with proper type inference
- Copy the output into your Gleam project under the src/ directory matching your module path
Why Use This Tool?
Tips & Best Practices
- Gleam custom types use PascalCase for type names and snake_case for field names — the generator follows these conventions
- When "Use Option for nullable fields" is enabled, null values become Option(T) with the gleam/option module imported automatically
- Empty arrays are typed as List(a) — you may need to specialize this to a concrete type like List(String) in production
- Gleam does not have null references — Option is the idiomatic way to represent nullable values
- Use the gleam/json package for encoding and decoding JSON to and from your generated custom types
Frequently Asked Questions
How are JSON types mapped to Gleam types?
JSON strings become String, integers become Int, floats become Float, booleans become Bool, null becomes Option(T) or Nil depending on the option toggle, empty arrays become List(a), and non-empty arrays become List(ElementType). Nested objects generate separate custom type definitions with their own named fields.
What is a custom type in Gleam?
Custom types in Gleam are algebraic data types that can hold data in named fields. They are similar to structs in Rust or records in Elm. Each custom type can have one or more variants, and each variant can have positional or named arguments. The generated types use a single variant with named fields, which is the standard pattern for data modeling in Gleam.
When should I NOT use Gleam custom types for JSON?
Avoid generated custom types when your JSON has highly dynamic or unpredictable fields, since Gleam custom types require a fixed set of fields. For APIs with varying response shapes, consider using Gleam's dynamic module for runtime-typed access. Also, if you need to handle JSON with unknown structure, use gleam/dynamic.Dynamic for flexible parsing.
How are nested objects handled?
Each nested JSON object becomes its own Gleam custom type with a PascalCase name derived from the field name. Types are ordered children-first so that referenced types are defined before the types that use them. The parent type references the child by its type name directly.
What is the Option type for nullable fields?
When "Use Option for nullable fields" is enabled, JSON null values are typed as Option(T) where T is the inferred type. The gleam/option module is automatically imported with None and Some constructors. This follows Gleam convention of using Option for nullable values instead of null references, ensuring safe access through pattern matching.
Is my JSON data sent to a server?
No. All code generation happens entirely in your browser using client-side JavaScript. Your JSON data never leaves your device, so you can safely convert sensitive API responses or proprietary data structures.
Real-world Examples
API Response Type for Gleam Backend
Generate Gleam custom types from a JSON API response for type-safe handling in your Gleam web application.
{"id": 1, "name": "Alice", "email": "alice@example.com", "is_active": true, "address": {"street": "123 Main St", "city": "Springfield"}}import gleam/option.{type Option, None, Some}
pub type Address {
Address(
street: String,
city: String,
)
}
pub type Root {
Root(
id: Int,
name: String,
email: String,
is_active: Bool,
address: Address,
)
}Nullable Fields with Option Type
Model a JSON payload where some fields may be null, using Gleam Option types for safe access.
{"title": "Draft", "score": null, "tags": ["review"]}import gleam/option.{type Option, None, Some}
pub type Root {
Root(
title: String,
score: Option(Int),
tags: List(String),
)
}