JSON to OCaml Type Generator

Generate OCaml type definitions from JSON data with automatic type inference.

What is JSON to OCaml Type Generator?

This tool converts JSON data into OCaml type definitions. It automatically infers OCaml types from JSON values, generates properly formatted record types, and handles nested objects and arrays with full type safety.

How to Use

  1. Paste your JSON data into the input area
  2. Click "Generate" to create OCaml type definitions
  3. Copy the output and use it in your OCaml project

Why Use This Tool?

Automatically infer OCaml types from JSON values (string, int, float, bool)
Generate clean OCaml record type definitions
Handle nested objects as separate type definitions
Support for option types (null → 'a option)
Support for list types (arrays → type list)
Follow OCaml naming conventions (snake_case fields)

Tips & Best Practices

  • Use realistic sample data — type inference depends on actual values
  • Nested objects become separate type definitions with PascalCase names
  • Field names are automatically converted to snake_case
  • Null values are typed as 'a option for flexibility
  • Empty arrays are typed as 'a list

Frequently Asked Questions

How are JSON types mapped to OCaml?

JSON strings → string, integers → int, floats → float, booleans → bool, null → 'a option, arrays → type list, objects → record types with { field : type; } syntax.

How are nested objects handled?

Nested JSON objects are converted to separate OCaml type definitions. They are ordered children-first so that referenced types are defined before the types that use them.

What about field naming conventions?

Field names are automatically converted to snake_case following OCaml conventions. For example, "isActive" becomes "is_active", "userName" becomes "user_name".

How are null values handled?

Null values are typed as 'a option, which is a polymorphic option type in OCaml. This provides flexibility while maintaining type safety.

Is my data sent to a server?

No, all processing happens entirely in your browser. Your JSON data never leaves your device.

Related Tools