Skip to content
Devtoolry

JSON to C#

Convert JSON to C# classes or records online with System.Text.Json [JsonPropertyName] attributes, nullable types and List properties for arrays.

Something not working, or missing a feature? Send feedback

About the JSON to C#

The JSON to C# converter generates classes or positional records for System.Text.Json from a JSON sample. Property names use PascalCase with [JsonPropertyName] attributes, arrays become List<T>, and values that were null or missing become nullable types.

Conversion happens in your browser, so your files and records stay private.

How to use the JSON to C#

  1. Paste a JSON sample and set the root class name.
  2. Choose classes or records.
  3. Copy the code into your project and deserialize with JsonSerializer.Deserialize<T>().

Example

C# classes from a user with projects

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

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

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

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

    [JsonPropertyName("email")]
    public object? Email { get; set; }

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

    [JsonPropertyName("team")]
    public Team Team { get; set; } = new();

    [JsonPropertyName("projects")]
    public List<Project> Projects { get; set; } = new();
}

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

    [JsonPropertyName("year")]
    public long Year { get; set; }

    [JsonPropertyName("archived")]
    public bool? Archived { get; set; }
}

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

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

System.Text.Json matches names case-sensitively, so every property gets [JsonPropertyName]. Non-null strings and lists are initialised, so nullable reference types report no warnings.

Common mistakes

  • Removing [JsonPropertyName]. Without it, System.Text.Json looks for "Id" and finds nothing for "id", leaving default values. Keep the attributes or set PropertyNameCaseInsensitive = true.
  • Assuming long is enough. JSON numbers above 9,223,372,036,854,775,807 or with a fraction fail to deserialize into long. Use decimal or double for money and measurements.

Works well with the JSON to C#

Frequently asked questions

Why does every property have [JsonPropertyName]?

System.Text.Json matches property names case-sensitively by default, so Id would not match "id". The attribute maps each property to its exact JSON key.

Does it work with Newtonsoft.Json?

Replace [JsonPropertyName] with [JsonProperty] and the using directive with Newtonsoft.Json; the rest of the code is the same.

What about nullable reference types?

Values that can be null are marked with ?, and non-null strings and lists are initialised (string.Empty, new()), so the code compiles without nullable warnings.