C#
.NET & APIs

JSON to C# Classes

Generate C# models for API responses.

Jump to the quick guide

Workspace

No sign-up

Types are inferred from sample values. Review generated code before using it.

Clear the text, result and validation messages.

Result

Your result will appear here.
Local processing: your input is never uploaded to our server.
DOCUMENTATION

What does this tool do?

Generate C# class definitions from a sample JSON object. Use them as a starting point for API response models, DTOs and tests in .NET projects. The tool creates a Root class, classes for nested objects and properties with JsonPropertyName to preserve JSON names. Processing happens in your browser; review inferred types before using generated code in production.

How to use it

  1. Paste a valid JSON object that represents the response you want to model.
  2. Select Generate C# classes and review Root, nested classes and JsonPropertyName attributes.
  3. Select Copy result and paste the definitions into a .cs file in your .NET project; adjust the namespace for your project.
  4. Review nullability, collections, numbers and names. Initialize references where appropriate and test deserialization with JsonSerializer.

Input and output examples

Order with a nested object, array and null value

Input

{"order_id":42,"total":19.95,"customer":{"name":"Ada"},"tags":["paid"],"note":null}

Output

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

public class Root
{
    [JsonPropertyName("order_id")]
    public int OrderId { get; set; }

    [JsonPropertyName("total")]
    public double Total { get; set; }

    [JsonPropertyName("customer")]
    public Customer Customer { get; set; }

    [JsonPropertyName("tags")]
    public List<string> Tags { get; set; }

    [JsonPropertyName("note")]
    public object? Note { get; set; }
}

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

Important limitations

  • Requires an object at the root; a root array, null or primitive is not accepted. It does not read schemas or merge multiple samples.
  • Infers string, bool and int for 32-bit integers; other numbers become double. It does not infer decimal, long, dates, enums or GUIDs from strings. JSON parsing can lose precision for large numbers.
  • A null value generates object?: it cannot reveal the actual type. Missing properties, required fields and nullability of future values are not inferred.
  • Arrays use only the first non-null element to infer List<T>. An empty or all-null array generates List<object>; types and properties from later elements are not merged.
  • String properties are initialized with string.Empty; object and list references are not initialized. Review nullable reference warnings, name collisions and compatibility with your C# settings.

Common mistakes

  • Pasting invalid JSON or a root list of objects. Select a representative object from the list to generate the model.
  • Assuming a single sample describes every variant of an API.
  • Using double for monetary values without checking whether the contract needs decimal.
  • Using generated classes without reviewing nullability, names, initialization or deserialization with real data.

Frequently asked questions

Are original JSON property names preserved?

Yes, through JsonPropertyName. C# identifiers are built from alphanumeric segments and receive suffixes where needed to avoid certain collisions. Review names before integrating the code.

How are nested objects and arrays handled?

Nested objects generate additional classes. Arrays generate List<T> from the first non-null element, including nested objects; later elements do not extend the model.

Why does a field become object?

A null value produces object? because the sample does not reveal its type. An empty array or one with no non-null elements produces List<object>. Replace these types after reviewing the API contract.

Can I paste the result directly into .NET?

The output includes using directives for collections and JsonPropertyName, and uses System.Text.Json. It is a draft model: review types, nullability and compiler warnings, add your namespace and test deserialization.