JSON to C# Class

Generate a C# class with JsonPropertyName attributes from a JSON document.

JSON Input
C# Class Output
C# Class output appears here

Related Tools

Documentation

What is JSON to C#?

This tool converts a JSON sample into C# classes with auto-property syntax ({ get; set; }), annotated for System.Text.Json — the JSON serializer built into .NET since .NET Core 3.0. It's a fast way to turn an API response or config payload into a strongly-typed model without writing the class by hand.

How it works

Each nested JSON object becomes its own public class, printed before any class that references it. Property names are PascalCased from the JSON key, per C# convention. Because a PascalCased property name (Id) almost always differs from the raw JSON key (id), the generator adds a [JsonPropertyName("...")] attribute above nearly every property so serialization still round-trips to the exact original key. A field whose sample value was null is typed with a nullable suffix (T?). Numbers are int or double depending on whether the sample value had a decimal point, and arrays are List<T> from System.Collections.Generic.

Features

  • PascalCase auto-properties with { get; set; }, matching standard C# conventions
  • [JsonPropertyName] attributes wired up automatically wherever the property name differs from the JSON key
  • Nested objects become separate classes; arrays become List<T>
  • Nullable value/reference types (T?) for fields observed as null
  • Required using directives included at the top of the output, ready to paste into a file

Example

Input: { "id": 101, "username": "alice_dev", "isActive": true, "signupBonus": null, "address": { "city": "Berlin", "zipCode": "10115" }, "tags": ["admin", "beta"] }

Output:

using System.Collections.Generic;
using System.Text.Json.Serialization;

public class Address
{
    [JsonPropertyName("city")]
    public string City { get; set; }

    [JsonPropertyName("zipCode")]
    public string ZipCode { get; set; }
}

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

    [JsonPropertyName("username")]
    public string Username { get; set; }

    [JsonPropertyName("isActive")]
    public bool IsActive { get; set; }

    [JsonPropertyName("signupBonus")]
    public object? SignupBonus { get; set; }

    [JsonPropertyName("address")]
    public Address Address { get; set; }

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

Common errors

A null sample value produces object? since there's nothing concrete to infer — narrow it once you know the real type. If your project still uses Newtonsoft.Json instead of System.Text.Json, swap every [JsonPropertyName("...")] for [JsonProperty("...")] — the class shape is otherwise identical. An empty array in the sample has no element to infer a type from and falls back to List<object>.

Best practices

Consider converting the generated class into a C# record if the model should be immutable — records keep the same property syntax but add value-based equality for free. Enable nullable reference types (<Nullable>enable</Nullable>) in your project so the ? annotations the generator emits are actually enforced by the compiler rather than being purely documentational.

Frequently Asked Questions

Why System.Text.Json attributes instead of Newtonsoft.Json?

System.Text.Json is the built-in serializer since .NET Core 3.0 and requires no extra package. If your project still uses Newtonsoft.Json, swap [JsonPropertyName("...")] for [JsonProperty("...")] — the class shape itself is identical either way.

When does a property get the JsonPropertyName attribute?

Only when the PascalCase C# property name doesn't match the original JSON key exactly (e.g. a JSON key that's camelCase or snake_case) — that's when the explicit mapping is needed so serialization round-trips correctly.

How are nested objects and arrays typed?

Nested objects become their own public class, and arrays are typed as List, using System.Collections.Generic — both imports are included at the top of the generated output.

Are properties nullable?

A property whose sample value was null in the input is generated as a nullable type (T?), matching C#'s nullable reference/value type conventions.