JSON to C# Classes

Generate C# classes from JSON with PascalCase properties, [JsonPropertyName] attributes, nullable reference types and List<T> collections. Free and private.

Uses System.Text.Json attributes and nullable reference types (string?, long?).

Runs entirely in your browser. Your data is never uploaded.

Related Tools

View All

What is JSON to C#?

Turn a JSON sample into C# classes for System.Text.Json. Every property is PascalCase, carries a [JsonPropertyName("…")] attribute with the original key and uses nullable types where the sample contains null or missing values.

How to use JSON to C#

  1. Paste a JSON sample into JSON input (or click Load sample / Open file).
  2. Set the Root type name (default Root).
  3. Copy the generated C# code or Download it as a file.

Example

Input
{"id":1,"name":"A","tags":["x"]}
Output
using System.Collections.Generic;
using System.Text.Json.Serialization;

public class Root
{
    [JsonPropertyName("id")]
    public long Id { get; set; }

    [JsonPropertyName("name")]
    public string Name { get; set; } = string.Empty;

    [JsonPropertyName("tags")]
    public List<string> Tags { get; set; } = new();
}

When to use JSON to C#

Use it for .NET HTTP clients, ASP.NET models or test data. For Newtonsoft.Json replace the attribute with [JsonProperty]; for money values consider changing double to decimal.

How JSON to C# works

The sample is walked recursively and every position collects the types it has seen. Arrays of objects are merged into one type: a key missing from some elements becomes optional, a key that is null somewhere becomes nullable, integers mixed with decimals become a floating-point type, and anything else that mixes types falls back to a dynamic type. Nested objects become their own named types (PascalCase of the key, array keys singularized: orders → Order), with numeric suffixes when names collide. C# output maps integers to long, decimals to double, strings to string, booleans to bool, arrays to List<T>, nested objects to their own classes and dynamic values to object?. Nullable or optional values get ?; non-nullable strings are initialized to string.Empty and lists or objects to new(), so the code compiles cleanly with nullable reference types enabled.

Common Use Cases

  • Deserialize an API response with HttpClient and System.Text.Json
  • Create request/response models for an ASP.NET controller
  • Model JSON configuration sections
  • Generate classes for Azure Function triggers

FAQs

Why is every property annotated?

The attribute keeps the exact JSON key even when the C# name differs, so serialization round-trips regardless of naming policy.

Why double instead of decimal?

JSON has no decimal type; double matches what the parser produces. Use decimal for currency values.

Does it work with Newtonsoft.Json?

Yes after replacing [JsonPropertyName] with [JsonProperty] and the using directive.

Explore more categories