JSON to C# Converter

Convert JSON data to C# classes with properties and attributes

JSON Input

Loading editor...

C# Output

C# code will appear here

Paste JSON in the input area to get started

JSON to C#: Classes for JsonConvert and Newtonsoft.Json

Paste a JSON response and get plain C# classes with [JsonProperty] attributes, ready for JsonConvert.DeserializeObject.

Step 1

Input Your JSON Data

Start by adding your JSON data that needs to be converted to C# classes. The tool analyzes the structure to generate appropriate POCOs.

Example: Try This JSON Data

Copy and paste this JSON example to see how it works:

{
  "customer": {
    "customerId": 12345,
    "firstName": "Anna",
    "lastName": "Kowalski",
    "email": "[email protected]",
    "isActive": true,
    "preferences": [
      "email_notifications",
      "newsletter",
      "promotions"
    ],
    "billing": {
      "street": "ul. Marszałkowska 1",
      "city": "Warsaw",
      "country": "Poland",
      "postalCode": "00-624"
    }
  }
}
Step 2

Check the types it picked

There are no settings to fiddle with. The C# classes are built from what is actually in your JSON, so it pays to paste a realistic sample with all the fields you expect.

•
Numbers: int when it fits in 32 bits, otherwise long, ulong or decimal; anything with a decimal point is double
•
Lists: every item is looked at, so fields that show up only in later items are not lost
•
Missing or null: number and bool properties become nullable, like int?
•
Names: PascalCase properties, with the original key kept in a JsonProperty attribute
Step 3

Read the generated classes

The Step 1 example comes out as three classes: Root, Customer and Billing.

C# Code Output

// Generated from your JSON. Needs the Newtonsoft.Json NuGet package. Read it with:
//   var data = JsonConvert.DeserializeObject<Root>(json);

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

namespace JsonToCSharp
{
    public class Root
    {
        [JsonProperty("customer")]
        public Customer Customer { get; set; }
    }

    public class Customer
    {
        [JsonProperty("customerId")]
        public int CustomerId { get; set; }

        [JsonProperty("firstName")]
        public string FirstName { get; set; }

        [JsonProperty("lastName")]
        public string LastName { get; set; }

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

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

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

        [JsonProperty("billing")]
        public Billing Billing { get; set; }
    }

    public class Billing
    {
        [JsonProperty("street")]
        public string Street { get; set; }

        [JsonProperty("city")]
        public string City { get; set; }

        [JsonProperty("country")]
        public string Country { get; set; }

        [JsonProperty("postalCode")]
        public string PostalCode { get; set; }
    }
}
Step 4

Copy or Download C# Code

Drop the classes into your .NET project and add the Newtonsoft.Json package from NuGet.

•
Copy the code straight into a file in your C# project
•
Download it as GeneratedClasses.cs
•
Read your JSON with JsonConvert.DeserializeObject<Root>(json), for example in an ASP.NET Core service that calls an outside API

What is JSON to C# Conversion?

C# is statically typed, so before you can work with a JSON response comfortably you need classes that describe its shape. Writing them by hand for a large payload is tedious and easy to get wrong, especially when some fields only appear in a few records.

This page writes those classes for you from a real sample. If you only want to tidy up or check the JSON first, try our JSON Formatter or JSON Validator.

Frequently Asked Questions

Which attributes does the generated code use?

Every property gets a Newtonsoft.Json [JsonProperty("name")] attribute holding the exact JSON key, so the C# property can follow PascalCase while still reading "first_name" or "customer-id". You need the Newtonsoft.Json NuGet package. If you use System.Text.Json instead, swap each [JsonProperty] for [JsonPropertyName] from System.Text.Json.Serialization.

How does it decide between int, long and double?

It looks at how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become int, bigger ones become long (an ID like 8901240544102066246 keeps every digit), then ulong, then decimal. A number written with a decimal point or exponent, such as 14.50 or 1e3, becomes double. Text in quotes, like "00501", stays a string so the leading zeros survive.

What happens when list items have different fields?

All items are merged into one class. A field that only some items have, or that is null in some of them, is still included; number and bool properties become nullable (int?, double?, bool?) in that case. If the same field holds different kinds of values, for example a number in one item and text in another, its type becomes object.

How are odd JSON keys turned into C# property names?

Keys are split into words and joined in PascalCase, so "first-name" and "first_name" both start as FirstName and the second one becomes FirstName2. A key that starts with a digit gets an X in front, and names that would clash with the class itself or with members like GetType get a number added. The [JsonProperty] attribute keeps the original key, so reading the JSON still works.

Can I change the name of the root class?

Not on this page: the top-level class is always called Root. Rename it in your editor after copying; nothing else in the generated code depends on the name apart from the class declaration itself.

Is my JSON sent anywhere?

No. The classes are generated in your browser as you type, and nothing you paste or upload is sent to our server.