{}JSONLint.app

JSON to C# Converter

Generate C# classes from JSON data. Paste your JSON and configure options:

Loading...
Loading...

Generate C# Classes from JSON

This tool automatically generates C# class definitions from your JSON data. It infers types from values, handles nested objects, and supports both Newtonsoft.Json and System.Text.Json serialization attributes.

Output Options

Standard POCO Classes

The default output generates plain old CLR object (POCO) classes with properties:

public class User
{
    [JsonProperty("name")]
    public string Name { get; set; }

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

C# 9+ Records

Enable "Use Records" for immutable, concise data types. Records are ideal for DTOs and API response models:

public record User(string Name, int Age);

Newtonsoft.Json vs System.Text.Json

Choose the serialization library your project uses:

  • Newtonsoft.Json (default) — Uses [JsonProperty] attributes. Most compatible, widely used in older projects.
  • System.Text.Json — Uses [JsonPropertyName] attributes. Built into .NET Core 3.0+ and .NET 5+, better performance.

Type Inference

The converter automatically infers C# types from JSON values:

JSON TypeC# TypeNotes
stringstring
integerint or longUses long for values exceeding int range
decimaldouble
booleanbool
nullobject?Nullable reference type
arrayList<T>Type inferred from first element
objectNested classGenerates separate class definition

Example Conversion

Given this JSON:

{
  "id": 1,
  "email": "user@example.com",
  "profile": {
    "displayName": "John",
    "avatarUrl": null
  },
  "roles": ["admin", "user"]
}

The converter generates:

using Newtonsoft.Json;
using System.Collections.Generic;

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

    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("profile")]
    public Profile Profile { get; set; }

    [JsonProperty("roles")]
    public List<string> Roles { get; set; }
}

public class Profile
{
    [JsonProperty("displayName")]
    public string DisplayName { get; set; }

    [JsonProperty("avatarUrl")]
    public object? AvatarUrl { get; set; }
}

Using Generated Classes

Deserializing JSON with Newtonsoft.Json

using Newtonsoft.Json;

string json = GetJsonFromApi();
var user = JsonConvert.DeserializeObject<Root>(json);
Console.WriteLine(user.Email);

Deserializing with System.Text.Json

using System.Text.Json;

string json = GetJsonFromApi();
var user = JsonSerializer.Deserialize<Root>(json);
Console.WriteLine(user.Email);

Configuration Tips

  • PascalCase conversion — Enable to convert userName toUserName following C# conventions
  • Namespace — Add a namespace wrapper for better code organization
  • JSON attributes — Enable when property names differ from JSON keys, or disable for cleaner code when names match

Common Issues

Empty arrays

Empty arrays generate List<object> since the element type can't be inferred. Update to the correct type in your code.

Mixed-type arrays

If an array contains different types, the converter uses List<object>. Consider using separate properties or a discriminated union pattern.

Reserved keywords

If your JSON contains C# reserved keywords as keys (like "class" or "event"), you'll need to manually add the @ prefix to the property name.

Related Tools

Frequently Asked Questions

Should I use Newtonsoft.Json or System.Text.Json?

For new .NET 5+ projects, System.Text.Json is recommended — it's built-in and faster. Use Newtonsoft.Json for older projects or when you need its advanced features (custom converters, LINQ to JSON, etc.).

What's the difference between classes and records?

Records (C# 9+) are immutable by default and provide built-in value equality. They're ideal for DTOs. Classes are mutable and better for entities that change over time.

How do I handle nullable types?

The converter uses nullable reference types (e.g., string?) for null values. Enable nullable reference types in your project with<Nullable>enable</Nullable> in your .csproj file.

Can I add validation attributes?

The converter generates basic class structure only. Add [Required],[MaxLength], and other validation attributes manually as needed.

Common Mistakes & Pro Tips

  • Nullable reference types change what null meansIf your project has `<Nullable>enable</Nullable>`, a `string` is non-nullable and the compiler warns when it could be null, while `string?` permits null. JSON fields that may be null or absent should be declared nullable (`string?`, `int?`) so deserialization doesn't produce warnings or unexpected nulls. Value types like `int` also need `?` to hold null.
  • PascalCase properties need a naming policy or attributeC# properties are PascalCase (`FirstName`) but JSON is often camelCase or snake_case. With System.Text.Json set `JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase` (and `SnakeCaseLower` in .NET 8+), or annotate individual properties with `[JsonPropertyName("first_name")]`. Newtonsoft uses `[JsonProperty("first_name")]` instead.
  • System.Text.Json and Newtonsoft attributes differThe two serializers use different namespaces and attributes: `System.Text.Json.Serialization`'s `[JsonPropertyName]` versus `Newtonsoft.Json`'s `[JsonProperty]`. They are not interchangeable, so generate for the serializer you actually use. Modern ASP.NET Core defaults to System.Text.Json.
  • Integers may need long, decimals may need decimalA whole-number sample is usually inferred as `int` (32-bit); use `long` for large IDs/timestamps. For fractional values, `double` is fast but imprecise — use `decimal` for money to avoid binary rounding. System.Text.Json maps these correctly as long as the property type matches the value's range.
  • Empty arrays, dates, and reserved wordsAn empty array `[]` has no element type and falls back to `object[]` or `List<object>` — replace with the real type. Date strings stay `string` unless you type the property `DateTime`/`DateTimeOffset`, which System.Text.Json parses from ISO 8601. Keys matching C# keywords (`class`, `event`, `params`) need either a `@`-prefixed name or a renamed property plus `[JsonPropertyName]`.

Frequently Asked Questions

Should I generate classes or records?

C# 9+ `record` types are ideal for immutable JSON DTOs — concise, with value equality and `with`-expressions — and System.Text.Json supports init-only and positional records. Mutable `class` types with `{ get; set; }` are best when you build the object incrementally or need a parameterless constructor. Use `record` for read-mostly API models and `class` when you need mutability.

How do I handle optional or nullable fields?

Make the property nullable: `int?`, `DateTime?`, or `string?` (with nullable reference types enabled). System.Text.Json leaves missing keys at their default (null for nullable types) and won't throw. To require a property, use the `required` modifier (.NET 7+) or `[JsonRequired]`; to ignore unknown JSON keys, no setting is needed — extra keys are skipped by default.

System.Text.Json or Newtonsoft.Json — which should I target?

System.Text.Json is built into .NET Core/.NET 5+, faster, and the default in ASP.NET Core, so prefer it for new code. Newtonsoft.Json (Json.NET) is more feature-rich (e.g. `TypeNameHandling`, more flexible converters) and common in older projects. The generated property attributes differ between them, so pick the one matching your codebase.

Why aren't my camelCase JSON keys matching by default?

By default System.Text.Json matches property names case-sensitively, so `firstName` won't bind to `FirstName`. Set `PropertyNamingPolicy = JsonNamingPolicy.CamelCase` or `PropertyNameCaseInsensitive = true` in `JsonSerializerOptions`, or add `[JsonPropertyName("firstName")]` per property. ASP.NET Core applies the camelCase policy automatically for request/response binding.

How are nested objects and collections represented?

Each nested object becomes its own class/record, referenced by the parent, and JSON arrays become `List<T>` or `T[]`. You can usually choose between `List<T>` (mutable, growable) and arrays; `List<T>` is the common default for deserialized collections. Dictionaries with dynamic string keys map to `Dictionary<string, T>`.

Does my JSON leave my browser?

No. The conversion runs entirely client-side in JavaScript, so nothing is uploaded — your JSON stays on your machine. That makes it safe to paste internal DTOs or payloads containing secrets.