JSON to C# Class

Paste a JSON sample and get back C# POCO classes ready to paste into a .cs file. Picks sensible types, handles nested objects with additional class definitions, supports nullable reference types, and emits either System.Text.Json or Newtonsoft.Json attributes depending on your project.

How to convert JSON to C#

  1. 1

    Paste the JSON

    A single sample is enough. Multiple samples improve nullable and array-item inference.

  2. 2

    Pick the style

    System.Text.Json (.NET 6+) or Newtonsoft.Json (legacy). PascalCase property names with `[JsonPropertyName]` for camelCase JSON.

  3. 3

    Pick target C# version

    C# 10+ for records and file-scoped namespaces, C# 8 for nullable reference types, or older for maximum compatibility.

  4. 4

    Copy the classes

    One root class plus nested classes for each object shape, all in one file ready to drop into your project.

Example output

For input:

{ "firstName": "Alice", "age": 30, "emails": ["a@a.com"], "address": { "city": "Madrid" } }

System.Text.Json output (C# 10+):

public class User
{
    [JsonPropertyName("firstName")]
    public string FirstName { get; set; } = default!;

    [JsonPropertyName("age")]
    public int Age { get; set; }

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

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

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

Type mapping

JSON C# type
string string
integer int (or long for > int.MaxValue)
number (decimal) double (or decimal if opted in)
boolean bool
null object? (or merged with sibling)
ISO-8601 date DateTime (or DateOnly)
GUID-shaped string Guid
array of strings List<string>
object Nested class

Attribute options

  • System.Text.Json ([JsonPropertyName("foo")]), preferred for .NET 6+ and new projects.
  • Newtonsoft.Json ([JsonProperty("foo")]), for legacy projects or when you need Newtonsoft-specific features.
  • None, property names match JSON keys exactly (works only if JSON keys are PascalCase already).

Common mistakes

  • Using int for a field that might overflow. If your JSON has values that exceed int.MaxValue, use long. The generator promotes automatically when it sees large values.
  • Missing [JsonIgnore] for computed properties. If you add helper props to the generated class, decorate them with [JsonIgnore] or they will serialize on the way out.
  • Forgetting culture-invariant parsing. decimal fields should deserialize with CultureInfo.InvariantCulture; System.Text.Json does this by default, Newtonsoft with global settings.
  • Trusting the generator with one sample. Nullability and array-item types are guessed from what it sees. Always sanity-check nullable annotations against your actual API behaviour.

Frequently Asked Questions

For new projects on .NET 6+, use System.Text.Json, it is faster, built-in, and now supports nearly every feature Newtonsoft does. Use Newtonsoft for legacy projects or when you need its specific features (custom contract resolvers, JObject, dynamic handling).

Records are idiomatic for immutable DTOs in C# 10+. They give you value equality and concise syntax. Classes are better when you need mutation or legacy compatibility. The tool lets you pick either.

If your project uses nullable reference types (C# 8+), fields observed as null in any sample become string?, int?, etc. Without NRT, nullability is signalled only for value types (e.g. int?).

Mixed arrays of different object shapes are not directly representable in strongly-typed C#. The tool infers a common base class or (fallback) object; for mixed arrays you usually want to redesign the JSON.

Related Tools

Tool available in other languages