Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

JSON to C# Class

JSON

Paste JSON, or

CtrlV paste · Ctrl↵ run now

C#
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Turn a JSON sample into C# records or classes: PascalCase properties, JsonPropertyName, nullable types and required members

Paste an API response and get C# records or classes, ready for System.Text.Json to read.

How to use it

  1. Paste a JSON sample, or open a .json file.
  2. Choose positional records or classes with properties, and set the root type’s name.
  3. Copy the code or download Models.cs, then read it with JsonSerializer.Deserialize<Root>(json).

Examples

A record

Input
{"order_id": 1042, "paid": true, "shipped_at": null}
Result
using System.Text.Json.Serialization;

public record Root(
    [property: JsonPropertyName("order_id")] int OrderId,
    [property: JsonPropertyName("paid")] bool Paid,
    [property: JsonPropertyName("shipped_at")] object ShippedAt
);

Classes with an optional property

Input
{"items": [{"sku": "MUG-01", "qty": 2}, {"sku": "TEE-07", "qty": 1, "size": "M"}]}
Result
using System.Collections.Generic;
using System.Text.Json.Serialization;

public class Root
{
    [JsonPropertyName("items")]
    public required List<Item> Items { get; set; }
}

public class Item
{
    [JsonPropertyName("sku")]
    public required string Sku { get; set; }

    [JsonPropertyName("qty")]
    public required int Qty { get; set; }

    [JsonPropertyName("size")]
    public string? Size { get; set; }
}

Size is missing from one item, so it is string? and not required.

How the types are read

The sample is read with a few rules, the same for every language:

  • Every object gets its own named type; the name comes from its key (customer → Customer, line_items → LineItem).
  • The objects in one array are merged into one type, so a field missing from some of them is marked optional.
  • A field that is null in one place and a string in another is a nullable string. A field that is only ever null, or mixes kinds such as 1 and "a", gets the language’s “anything” type.
  • Integers and decimals are told apart by how they are written: 25 is an integer, 25.0 and 12.5 are not. An integer past 2,147,483,647 gets a 64-bit type, and one past 64 bits a big-number type, read from the digits, never rounded.
  • Empty arrays get a list of “anything”, since the sample doesn’t say what they hold.

C# specifics

  • Properties are PascalCase, and each has [JsonPropertyName] with the JSON key, because System.Text.Json matches names case-sensitively unless you tell it otherwise.
  • In classes, a property every object has is required (C# 11, .NET 7+), so reading JSON without it fails instead of leaving it empty.
  • Properties that may be missing or null are nullable (int?, string?).
  • Integers get int, long past 32 bits, and decimal past 64 bits; decimals get double.
  • A property can’t share its class’s name in C#, so one that would gets a 2 (a key root in Root becomes Root2).

Questions

Does it work with Newtonsoft.Json?
Yes, after changing [JsonPropertyName("key")] to [JsonProperty("key")] and the using line to Newtonsoft.Json.
What C# type does a big number get?
Integers get int, then long past 32 bits and decimal past 64 bits, so a large id keeps every digit; numbers with a fraction get double. A field that is missing or null somewhere in the sample gets the nullable type, such as long?.
Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.

Related tools