1. Home
  2. Developer tools
  3. JSON to C# Record

JSON to C# Record

Generate .NET models for an API response, with the attributes the built-in .NET serializer needs to map JSON keys to PascalCase properties.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use JSON to C# Record

  1. Paste JSON.
  2. Set the namespace and root type name.
  3. Copy the records into your project.

Features

  • sealed records with init-only properties
  • [JsonPropertyName] keeps every original key
  • required for fields present in every sample (C# 11)
  • Nullable ? types for optional and null fields
  • long, double, bool, string, List<T>, JsonElement; optional DateTimeOffset

Supported formats

You can open

  • JSON
  • JSON Lines
  • Typed or pasted text

You get

  • CS
  • Copied text

Private by design

Your files are opened and processed by your browser on this device. They are never uploaded to openWorkstand or anyone else, so the JSON to C# Record is safe for contracts, IDs, photos and work documents. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • required members need C# 11 (.NET 7) or later; remove the keyword for older versions.

Guide

C# records for the .NET JSON serializer from sample JSON

Visual Studio’s Paste JSON as Classes produces older-style classes without attributes or nullability. Modern .NET code usually wants records, System.Text.Json attributes and nullable reference types. This generator writes exactly that from a sample, so a response from any API can be deserialised in one line.

Built for the .NET serializer

The output targets System.Text.Json, the serializer built into .NET Core 3.0 and later and the default in ASP.NET Core. It is faster than Newtonsoft.Json and needs no package. Each property carries [JsonPropertyName("…")] with the original key, so the PascalCase C# names do not depend on serializer options.

Records with init-only properties

Every object shape becomes a public sealed record with { get; init; } properties. Records give value equality, so two orders with the same data compare equal, which makes tests simpler; init-only setters keep the objects immutable after deserialisation while still allowing object initialisers in your own code.

required and nullable

Properties present with a value in every sample are marked required, a C# 11 feature: the compiler insists they are set when you create the object yourself, and from .NET 7 System.Text.Json reports a missing required property as an error instead of leaving a default. Optional and nullable fields use nullable types such as string? and long? and are not required, so enable nullable reference types (<Nullable>enable</Nullable>) to get warnings where your code forgets to check them.

Types

Whole numbers map to long and decimals to double, booleans to bool, arrays to List<T>. Fields whose type varies or that were always null become JsonElement, which you can inspect with ValueKind. With date types on, ISO 8601 timestamps become DateTimeOffset, which keeps the time-zone offset.

Using the records

In ASP.NET Core minimal APIs, declare the record as a parameter and the body is bound for you: app.MapPost("/users", (User user) => …). With HttpClient, GetFromJsonAsync<User> and ReadFromJsonAsync<User> deserialise in one call. For source generation and trimming in AOT apps, add a JsonSerializerContext that lists the root record.

A worked example

The sample user becomes four records: User, Order, Item and Address. The order’s key order_id becomes [JsonPropertyName("order_id")] public required long OrderId { get; init; }, while the coupon that only one order has becomes public string? Coupon { get; init; } without required.

Older .NET versions

required needs C# 11 and .NET 7; for earlier versions delete the keyword. For projects on Newtonsoft.Json, replace the attribute with [JsonProperty("…")] and change the using directives. File-scoped namespaces need C# 10; switch to a block namespace for older compilers.

Collections and defaults

List<T> properties are required when every sample had the array, even if some arrays were empty, so code can iterate without null checks. If you prefer an empty list when the key is missing, drop required and initialise the property: public List<Item> Items { get; init; } = []; with C# 12 collection expressions.

Naming the root type

The root record takes the name you type, and nested records are named after their keys in singular form. Rename them freely in your editor; the attributes keep the JSON mapping intact, so renaming a C# type never changes the wire format.

Generate Java records with JSON to Java or TypeScript for the front end with JSON to TypeScript, translate curl examples into C# HttpClient code with cURL to Code, or inspect the payload in the JSON Formatter.

Checked against the current version of the JSON to C# Record on .

Troubleshooting

The file does not open

Check that it is one of the supported formats above and opens in another app. Damaged files and password-protected PDFs cannot be read until they are repaired or unlocked.

It is slow or stops on a very large file

Everything runs on your device, so speed depends on its memory. Close other tabs, use a computer rather than a phone, or split the file into smaller parts first.

The download did not start

Look for your browser’s download bar or icon. Some browsers ask once before a site may download files; allow it and click the button again.

Frequently asked questions

Why is every property annotated when PascalCase would match anyway?

The built-in .NET serializer is case-sensitive by default, so Name does not match "name" unless you set PropertyNameCaseInsensitive or a naming policy. Explicit attributes make the mapping work with any serializer options.

How do I use these with HttpClient?

Use the GetFromJsonAsync extension method on HttpClient, which downloads and deserialises in one call. It uses web defaults and the attributes above, and throws if the response is not valid JSON.

Can I use classes instead of records?

Yes. Replace sealed record with class; the properties and attributes stay the same. Records add value equality and a readable ToString, which is useful for DTOs and tests.

Do I need Newtonsoft?

No. The output targets the JSON serializer built into .NET Core 3.0 and later. For Newtonsoft, replace [JsonPropertyName] with [JsonProperty] and the using directives.

What does required do?

It makes the compiler insist the property is set when the object is created, and with .NET 7 or later the built-in serializer fails deserialisation when a required property is missing from the JSON.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.