JSON to F# Converter

Turn JSON into F# record types you can read with System.Text.Json

JSON Input

Loading editor...

F# Output

F# code will appear here

Paste JSON in the input area to get started

How to Convert JSON to F# Record Types - Step by Step Guide

Step 1

Paste or upload your JSON

Start with the JSON your API returns, or press Sample. This is the JSON the Sample button loads:

{
  "userId": 12345,
  "username": "fsharpdev",
  "email": "[email protected]",
  "isActive": true,
  "preferences": [
    "functional_programming",
    "immutability",
    "pattern_matching"
  ],
  "profile": {
    "firstName": "F#",
    "lastName": "Developer",
    "age": 31,
    "country": "Denmark"
  }
}
Step 2

Pick a type name and style

There are two settings above the editors. Type Name sets what the top-level type is called. Use Record Types switches between immutable F# records and plain classes with mutable properties, which some libraries and older code expect.

•
Records: structural equality and pattern matching come for free, and System.Text.Json fills them through their constructor
•
Nested objects: each one becomes its own record type, named after its key
•
Lists: become F# lists, and lists of objects get one record type merged from every item
Step 3

Read the generated types

For the Step 1 JSON the page writes these two types (below a short comment that shows how to read the JSON). The profile object became its own Profile record, and the preferences array became a string list.

type Root =
    { [<JsonPropertyName("userId")>] UserId: int
      [<JsonPropertyName("username")>] Username: string
      [<JsonPropertyName("email")>] Email: string
      [<JsonPropertyName("isActive")>] IsActive: bool
      [<JsonPropertyName("preferences")>] Preferences: string list
      [<JsonPropertyName("profile")>] Profile: Profile }

and Profile =
    { [<JsonPropertyName("firstName")>] FirstName: string
      [<JsonPropertyName("lastName")>] LastName: string
      [<JsonPropertyName("age")>] Age: int
      [<JsonPropertyName("country")>] Country: string }
Step 4

Copy or download the .fs file

Download GeneratedTypes.fs, add it to your project above the file that uses it, and read your JSON like this:

open System.Text.Json
open GeneratedTypes

let json = System.IO.File.ReadAllText "user.json"
let user = JsonSerializer.Deserialize<Root>(json)
printfn "%s lives in %s" user.Username user.Profile.Country

What is JSON to F# Conversion?

F# code usually wants typed data rather than a loose JSON tree. Writing the record types by hand for a large API response is slow and easy to get wrong, especially when some items leave fields out. This page reads your JSON and writes the record types for you, with field types that match the values it saw.

The output is plain F# for .NET. If your data is XML rather than JSON, the XML to F# converter writes records that read the XML with System.Xml.Linq.

Frequently Asked Questions

Which JSON library do the generated types work with?

System.Text.Json, the serializer that ships with .NET. Since .NET 6 it reads F# records, lists and options on its own, so no NuGet package is needed. Every field has a [<JsonPropertyName("...")>] attribute holding the exact JSON key. Newtonsoft.Json ignores that attribute and writes F# options in its own shape, so stick with System.Text.Json or rename the attributes yourself.

How are numbers typed?

From how each number is written in your JSON, not from a rounded copy. Whole numbers that fit 32 bits become int, bigger ones become int64 (so 8901240544102066246 keeps every digit), then uint64 and decimal for even larger values. Numbers with a decimal point or exponent, such as 14.50 or 1e3, become float. A quoted value like "00501" stays a string.

What happens with lists whose items differ?

Every item is looked at, not just the first one. A field that is missing or null in some items becomes an option (int option, string option and so on), a field that holds both whole and decimal numbers becomes float, and a field whose values have different kinds, like a number in one item and text in another, becomes JsonElement so nothing is lost.

What if my JSON keys are not valid F# names?

Field names are turned into PascalCase F# identifiers: first-name and first_name both become FirstName, a key starting with a digit gets Field in front, and two keys that would end up with the same name get a number added. The attribute keeps the original key, so the JSON is still read correctly. Type names that would hide built-in types, such as String or List, get Data added.

Can I convert a JSON array instead of an object?

Yes. A list of objects turns into one record type built from all the items, and the comment at the top tells you to read it as Root list. A single number, string or a list of plain values has nothing to build a record from, so the page says so instead of producing code.

Is my JSON sent anywhere?

No. The F# code is generated in your browser and the JSON is not uploaded to a server. Nothing is kept either: reloading the page starts with an empty editor.