JSON to F# Record Generator

Generate F# record type definitions from JSON data with automatic type inference.

What is JSON to F# Record Generator?

F# record types are immutable reference types with named fields and structural equality by default, making them the preferred way to model domain data in F# applications. This generator transforms your JSON data into idiomatic F# record type definitions with automatic type inference — strings become string, integers become int, floats become float, booleans become bool, and null values become 'a option for optional fields. Field names follow F# PascalCase conventions, and nested objects are extracted as separate record types with children-first ordering to satisfy the F# compiler's sequential type dependency requirements.

How to Use

  1. Paste your JSON object into the input area — the tool requires a JSON object, not an array or primitive
  2. Click "Generate" to produce F# record type definitions with proper type inference
  3. Review the generated code: nested objects appear as separate record types before the root type
  4. Copy the output into your F# project — typically in a Types.fs or Domain.fs file
  5. Add the Fable.Remoting.Json or Thoth.Json decoder for JSON serialization if needed

Why Use This Tool?

Generates idiomatic F# record types with structural equality and immutability built in
Automatic type inference maps JSON strings to string, integers to int, floats to float, and booleans to bool
Nested JSON objects become separate F# record types, keeping your domain model modular
PascalCase field naming follows the official F# style guide conventions
Null values are typed as 'a option, encouraging safe access patterns with Option.map and pattern matching
Children-first type ordering ensures the F# compiler resolves all type dependencies correctly

Tips & Best Practices

  • Provide realistic sample data because type inference is driven by actual JSON values — a whole number like 3.0 produces float, not int
  • Nested objects become separate record types with PascalCase names derived from the field name
  • Null values are typed as 'a option for flexibility — consider replacing with a concrete option type like string option in production
  • Empty arrays are typed as 'a list — you may need to specialize this to a concrete type like string list
  • For JSON serialization, use Thoth.Json or Fable.Remoting.Json which provide type-safe encoding and decoding

Frequently Asked Questions

How are JSON types mapped to F# types?

JSON strings become string, integers become int, floats become float, booleans become bool, null becomes 'a option (a generic option), empty arrays become 'a list, and non-empty arrays become T list where T is inferred from the first element. Nested objects generate separate record types with their own field definitions.

What is a record type in F# and how does it differ from a C# class?

F# records are immutable reference types with named fields, structural equality (two records with the same values are equal), and non-destructive mutation via the "with" keyword. Unlike C# classes, records provide default equality comparison, are sealed by default, and support pattern matching. They are ideal for domain modeling and DTOs in functional-first F# code.

When should I NOT use F# record types for JSON?

Avoid generated record types when your JSON has highly dynamic or unpredictable fields, since records require a fixed set of fields. For APIs with varying response shapes, consider using FSharp.Data JsonProvider for type providers, or System.Text.Json with JsonDocument for dynamic access. Also, if you need EF Core entity mapping with navigation properties, use F# class types with CLIMutable attribute instead.

How are nested objects handled?

Each nested JSON object becomes its own F# record 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, satisfying the F# compiler's sequential type resolution requirements.

Why are field names in PascalCase?

F# naming conventions use PascalCase for type names, public values, and record fields, as specified in the official F# style guide. This differs from JSON which typically uses camelCase or snake_case. You may need to add a JsonProperty attribute or configure your serializer to map between naming conventions.

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 Domain Model

Generate F# record types from a REST API response for type-safe access in your F# backend.

Input
{"id": 1, "name": "Alice", "email": "alice@example.com", "is_active": true, "address": {"street": "123 Main St", "city": "Springfield"}}
Output
type Address = {
    Street : string
    City : string
  }

type Root = {
    Id : int
    Name : string
    Email : string
    IsActive : bool
    Address : Address
  }

Optional Fields for Partial Data

Model a JSON payload where some fields may be null, using F# option types for safe access.

Input
{"title": "Draft", "score": null, "tags": ["review"]}
Output
type Root = {
    Title : string
    Score : 'a option
    Tags : string list
  }

Related Tools