JSON to F# Converter
F# record types from a JSON sample
JSON Input
F# Output
Paste your JSON on the left and the F# code shows up here.
Or drop a file on this box, , or .
What does the JSON to F# converter generate?
For poking at an API from a script, an F# type provider is hard to beat; for code you ship, explicit types read better in review. Paste one booking or order here and you get an F# record per object, PascalCase field names, and a [<JsonPropertyName("bookingRef")>] attribute on each field so System.Text.Json still finds the camelCase key.
Types are emitted innermost first, so the file compiles top to bottom with no and chains. Text is string, fractions float, true/false bool, and whole numbers int, int64, uint64 or decimal by the largest value present. Arrays turn into F# lists: Bag list, or (int list) list for an array of arrays. A key that only some array items carry, like cabin on the second bag, gets an option type, and a key seen only as null, or whose type changes between records, is typed JsonElement option so nothing is guessed.
The generator does not create discriminated unions or serializers; it gives you the record shapes. System.Text.Json in .NET 6 and later reads F# records, lists and options directly. For unions or finer control, the FSharp.SystemTextJson package is the usual choice. Everything here runs in your browser.
How to use it
Paste JSON and the records appear a moment later.
Paste a JSON sample
Paste into JSON Input, or bring in a saved response with Upload. Sample loads one flat subscriber record with four keys, enough to see the attribute style. A booking with a list and a few optional fields shows more of the mapping:
{"bookingRef":"KX7Q2P","passenger":"Linnea Berg","seat":"14C","fareEur":189.99,"bags":[{"kg":20},{"kg":8,"cabin":true}],"loyaltyNo":null} The root record takes its name from Type Name. Paste an array of objects and its items become that record, under a comment telling you to deserialize into Booking list. If the paste is rejected, the JSON Validator will show the broken line.
Read the records
The F# Output panel starts with the open lines it needs, then the records, innermost first. The booking above gives a Bag record (Kg: int, Cabin: bool option) ahead of RootObject, whose LoyaltyNo is JsonElement option because the sample only ever showed null there.
Copy or download
Copy to the clipboard or Download an .fs file, then deserialize with JsonSerializer.Deserialize<RootObject> json.
Where this saves time
Typed API clients
Capture a response from the service you call, generate the records, and replace JsonDocument lookups with pattern-matchable data.
Freezing a type-provider schema
Prototyped with a JSON type provider? Generate explicit records from the same sample so your build no longer depends on a sample file at compile time.
Scripts and notebooks
Paste the records into an .fsx script or a Polyglot notebook and work with typed data while exploring an export.
Finding the optional fields
Paste several real records as an array. Fields that are absent or null in any of them come out as option, which tells you where to handle None.
Common Questions
Does it generate discriminated unions?
No. JSON has no union tag to infer one from, so the output is records, lists and options only. Mixed-type values become JsonElement for you to match on.
Why do the fields have JsonPropertyName attributes?
F# field names are PascalCase while most JSON keys are camelCase, and System.Text.Json matches case-sensitively by default. The attribute keeps the original key, including keys like user-id that can't be F# names as they are.
How are big integers typed?
The page reads integers exactly, so an ICCID like 8901240544102066246 becomes int64 rather than a rounded float. Values above the uint64 range become decimal.
What about an empty object?
An empty {} says nothing about its keys, so it becomes Map<string, JsonElement>. If the whole root is empty, or null, or a bare value, the output explains why there is nothing to generate.
Is my data uploaded?
No. The records are generated by JavaScript in this page and nothing is sent to a server. Inputs over 256 KB are refused.
Other JSON Tools You Might Need
Working in C# too, or need to tidy the JSON first?