Xerobit

JSON to C# Class Converter

Paste a JSON object or array to generate C# classes with inferred types, PascalCase property names and optional JsonPropertyName attributes. The converter handles nested objects, arrays, nulls and Unicode characters. It runs entirely in the browser so nothing is uploaded to any server.

C# Classes

using System.Text.Json.Serialization;

public class RootObject
{
    [JsonPropertyName("name")]
    public string Name { get; set; }
    [JsonPropertyName("age")]
    public int Age { get; set; }
}

How to use the json to c# class converter

  1. Paste your JSON into the JSON input text area. The tool accepts a single object or an array of objects.
  2. Set the Root class name for the top-level C# class. Nested objects are named automatically from their keys.
  3. Optionally enter a Namespace to wrap all generated classes inside a namespace block.
  4. Choose whether to include [JsonPropertyName] or [JsonProperty] attributes for serialization.
  5. The C# code updates live as you type. Copy the output directly into your project.

How type inference works

The converter inspects every value in the JSON to pick the narrowest C# type. Strings become string, booleans become bool. Numbers without a fractional part that fit within 2,147,483,647 become int; larger integers become long. Any number with a decimal point becomes double.

When a key holds null in every object examined, the type defaults to object. If the nullable option is enabled, it renders as object?. When an array contains mixed types, the element type widens to object. For example, [1, "two"] produces List<object>.

Nested objects trigger generation of a separate class. The class name is derived by converting the JSON key to PascalCase. A key called shipping_address becomes the class ShippingAddress.

Property naming and attributes

C# convention uses PascalCase for public properties, while JSON typically uses camelCase or snake_case. This json to c# class converter transforms keys automatically: first_name becomes FirstName, and createdAt becomes CreatedAt.

Because the property name no longer matches the JSON key exactly, serializers need help mapping them back. Selecting System.Text.Json adds [JsonPropertyName("original_key")] above each property. Selecting Newtonsoft.Json adds [JsonProperty("original_key")] instead. If your serializer is configured to handle case-insensitive matching globally, you can choose None to keep the output minimal.

Common JSON to C# type mappings

JSON valueInferred C# typeNotes
"hello"stringUnicode characters are preserved
42intFits in 32-bit signed integer
3000000000longExceeds int.MaxValue
3.14doubleAny fractional part triggers double
trueboolLowercase in JSON
nullobjectBecomes object? if nullable is on
[1, 2]List<int>Element type inferred from contents
{"x": 1}Nested classClass name derived from the key

Handling arrays and duplicate class names

If the root JSON is an array, the converter examines every object in the array to build a unified schema. Keys present in some objects but missing in others are still included in the generated class. If a key maps to different types across objects (for instance, a string in one and a number in another), the type widens to object.

Nested objects with identical keys at different levels produce distinct classes. The converter tracks paths to avoid collisions. If two sibling keys both contain objects, they each receive their own class named after their respective keys. If a collision would occur because two different paths share the same PascalCase key name, a numeric suffix is appended to ensure every class name is unique.

Worked examples

ExampleInputResult
Simple user objectJSON input: {"name":"Ada","age":36,"active":true}; Root class name: User; Serialization attributes: system-text-json; Use nullable reference types (?): yesusing System.Text.Json.Serialization; / public class User / { / [JsonPropertyName("name")] / public string Name { get; set; } / [JsonPropertyName("age")] / public int Age { get; set; } / [JsonPropertyName("active")] / public bool Active { get; set; } / }
Nested object with arrayJSON input: {"id":1,"address":{"street":"123 Main St","city":"Austin"},"tags":["admin","user"]}; Root class name: Profile; Namespace: MyApp.Models; Serialization attributes: none; Use nullable reference types (?): nousing System.Collections.Generic; / namespace MyApp.Models / { / public class Profile / { / public int Id { get; set; } / public Address Address { get; set; } / public List<string> Tags { get; set; } / } / public class Address / { / public string Street { get; set; } / public string City { get; set; } / } / }
Array of objects with nullsJSON input: [{"title":"Post 1","views":150,"author":null},{"title":"Post 2","views":4200000000,"author":"Bob"}]; Root class name: BlogPost; Serialization attributes: newtonsoft; Use nullable reference types (?): yesusing Newtonsoft.Json; / public class BlogPost / { / [JsonProperty("title")] / public string Title { get; set; } / [JsonProperty("views")] / public long Views { get; set; } / [JsonProperty("author")] / public string? Author { get; set; } / }

FAQ

Does this tool upload my JSON to a server?

No. The json to c# class conversion happens entirely in your browser using JavaScript. Your data never leaves your device.

How are large numbers handled?

Integers up to 2,147,483,647 map to C# int. Larger integers map to long. Numbers with decimal points always map to double.

What happens if a JSON value is null?

If every observed value for a key is null, the type defaults to object. When the nullable option is checked, it is written as object? to support C# nullable reference types.

Can I use this with System.Text.Json and Newtonsoft.Json?

Yes. Choose the corresponding option in the Serialization attributes dropdown. The converter will add the correct attribute above each property so deserialization works out of the box.

How are nested objects named?

Each nested object generates its own class. The class name is created by converting the JSON key to PascalCase. For example, a key called billing_address becomes the class BillingAddress.

What if my JSON root is an array?

The converter merges all objects in the array to determine the complete set of properties. The resulting C# code represents a single item, which you can deserialize as a List of that class.