A type provider creates F# types from an external data sample while you edit code. Instead of generating a source file, it makes the inferred members available to the compiler, removing much of the manual work of defining DTO-style shapes for data you need to read. section 10.3 section 10.3.4
With the JSON type provider in FSharp.Data, a representative JSON value becomes a typed schema. A sample guitar exposes members such as Brand, Strings, and Pickups; changing Brand to Company in the sample makes existing uses of Brand fail immediately. The sample is therefore both a compact schema description and a useful compiler-checked contract. section 10.3.1 section 10.3.2
Infer a type from a JSON sample
Reference FSharp.Data, place a small JSON string in a literal value, and pass that value to JsonProvider. GetSample then returns a value whose fields can be accessed directly, so a description can be formed without manually declaring a guitar class. section 10.3.1
Listing 10.4 Working the the JSON type provider
#r "nuget:FSharp.Data"
open FSharp.Data
[<Literal>] #1
let SampleJson = """{
"Brand": "Ibanez",
"Strings": "7",
"Pickups": [ "H", "S", "H" ] }"""
type GuitarJson = JsonProvider<SampleJson> #2
let sample = GuitarJson.GetSample() #3
let description = $"{sample.Brand} has {sample.Strings} strings." #4
This short listing shows the complete loop: declare a literal JSON schema, create GuitarJson, load the sample, and use inferred properties. section 10.3.1
Keep the two phases distinct. The provider analyzes the sample at edit time to create a schema; GetSample or GetSamples loads data at runtime. The schema can come from an inline literal, a local file, or HTTP, while Load can read a different runtime resource that matches the established structure. section 10.3 section 10.3.2
Use samples deliberately to make inferred types useful and maintainable:
- Shape the schema by hand when real data varies. For a field that may be absent, include rows where it is present and missing so an option-like type can be inferred. section 10.3.4
- Store and source-control a stable schema file rather than relying on a live remote resource that may disappear or change unexpectedly. section 10.3.4
- Map provided classes into richer domain types instead of making them the core domain model; provided types are limited to classes, enums, and properties. section 10.3.4
- Use providers for scripts, rapid prototyping, and dataset exploration when building a full domain model would be unnecessary overhead. section 10.3.4 section 10.7
FSharp.Data also supplies CSV and HTML providers. An HTML provider can infer access to tables from a local or web page, after which ordinary F# collection functions can transform the rows. That output can flow directly into Plotly.NET, such as calculating London borough population density and rendering a column chart. section 10.3.3 section 10.4
Provided types are an F# feature rather than a shared interop contract: C# sees them only as objects, without their generated members. For durable JSON-to-domain boundaries, manual serialization with System.Text.Json and explicit mapping remains an alternative. section 11.2.1 section 10.7
