System.Text.Json is the built-in .NET library for converting JSON to and from F# values. It works best at the boundary of an application: deserialize into simple raw records, then validate and map those records into richer domain types. section 10.2.1 section 10.7
A practical workflow separates transport-shaped data from domain invariants:
- Validate required fields and convert raw values into discriminated unions and other domain types. section 10.2.1
- Serialize domain values by first mapping them into a JSON-shaped record; anonymous records can avoid output-only type declarations. section 10.2.3
Guitar inventory data
A guitar domain model can use Brand, Strings, Pickup, and Kind unions, while incoming JSON uses ordinary strings and arrays. Deserialize the JSON into RawGuitar, run tryAsFullGuitar, and obtain a validated Guitar or an error. For reporting, count guitars by brand, project anonymous records such as {| Brand = brand; Guitars = count |}, and pipe them to JsonSerializer.Serialize.


