What is JSON to OCaml Type Generator?
OCaml is a strongly typed functional programming language renowned for its expressive type system, pattern matching, and efficient native compilation. When consuming JSON APIs or reading configuration files in OCaml, you typically define record types that mirror the JSON structure and then write decoders to convert parsed JSON into those types. This tool automates the first step: it inspects your JSON data and produces OCaml record type definitions with accurate type annotations. Strings become string, integers become int, floating-point values become float, booleans become bool, null becomes 'a option, and arrays become 'a list. Nested JSON objects are extracted into their own record types with PascalCase names, and all field identifiers are converted to snake_case following OCaml conventions. The result is a set of type definitions you can paste directly into an .ml file and pair with libraries like yojson or atdgen for decoding.
How to Use
- Paste a representative JSON object into the input area on the left
- Click "Generate" to produce OCaml record type definitions in the output area
- Inspect the generated types — nested records appear before the types that reference them
- Copy the output into your .ml or .mli file and add decoding logic with your preferred JSON library
Why Use This Tool?
Tips & Best Practices
- Provide JSON that includes every field — omitted keys cannot be inferred and will be missing from the output types
- The 'a option type for nulls is polymorphic; replace 'a with the concrete type once you know what the field should contain
- Empty arrays produce 'a list — update the type parameter to match the actual element type your API returns
- OCaml record field names must be unique across all records in a module. If two generated records share a field name, you may need to split them into separate modules
- Pair the generated types with yojson's Yojson.Safe.from_string and pattern matching for robust JSON decoding
Frequently Asked Questions
How are JSON types mapped to OCaml types?
JSON strings become string, whole numbers become int, decimal numbers become float, booleans become bool, null becomes 'a option (a polymorphic option type), arrays become 'a list, and nested objects become separate record types defined with { field : type; } syntax.
When should I not use this generator?
Avoid this tool when your JSON uses union types — for example, a field that can be either a string or an integer. OCaml's type system requires variant types for such polymorphism, which this generator does not produce. Also, if you already have an OpenAPI or JSON Schema specification, use atdgen or ppx_deriving_yojson to generate both types and decoders from that schema instead.
Is my JSON data sent to a server?
No. All type inference and code generation runs entirely within your browser. Your JSON data is never transmitted to any external server, so it is safe to paste payloads containing sensitive information.
Why are null values typed as 'a option instead of a specific option?
When a JSON value is null, the concrete underlying type cannot be determined from that value alone. The generator uses 'a option as a placeholder. You should replace 'a with the actual type (e.g., string option or int option) based on your API documentation or other non-null samples.
How do I decode JSON into these generated types?
Use a library like yojson with pattern matching. For example, parse the JSON with Yojson.Safe.from_string, then pattern match on the resulting JSON value to construct your OCaml record. Alternatively, use atdgen to auto-generate decoders from a .atd specification file.
Can I use these types with BuckleScript or ReScript?
These types follow native OCaml syntax. BuckleScript/ReScript uses a different runtime and has its own JSON decoding libraries (like bs-json or @rescript/core). While the type shapes are similar, you would need to adapt the syntax and use ReScript-specific decoders.
Real-world Examples
Typing a REST API response for a task tracker
When building an OCaml backend that communicates with a task management API, you need record types matching the JSON responses. Paste a sample response to generate the types, then write yojson decoders to safely convert incoming JSON into typed OCaml values.
{
"taskId": 101,
"title": "Fix login bug",
"is_completed": false,
"priority_score": 3.7,
"assignee": null,
"tags": ["backend", "urgent"]
}type task = {
task_id : int;
title : string;
is_completed : bool;
priority_score : float;
assignee : 'a option;
tags : string list;
}Modeling a nested configuration object
OCaml services often read JSON configuration at startup. Generate typed records for each config section so the compiler enforces that every required field is accessed, preventing runtime Key_not_found errors.
{
"server": {
"host": "0.0.0.0",
"port": 8080
},
"database": {
"connection_string": "postgresql://localhost/mydb",
"pool_size": 10
}
}type server = {
host : string;
port : int;
}
type database = {
connection_string : string;
pool_size : int;
}
type config = {
server : server;
database : database;
}