dev cosmos/ blog/ json-to-csharp-guide

JSON to C# Class Generator: Stop Writing Boilerplate Forever

How to instantly convert any JSON payload into production-ready C# POCO classes — with attributes, nullable types, and XML documentation.

C#

JSON → C# Generator

POCO · Attributes · .NET

Every .NET developer has been there: you get a JSON payload from an API, and now you need a strongly-typed C# class to deserialise it into. Writing it by hand is tedious, error-prone, and wastes time you could spend on actual logic. The Dev Cosmos JSON → C# tool eliminates this toil entirely.

C#
Open in Dev Cosmos
JSON → C# Class Generator →

What Is a POCO Class?

POCO stands for Plain Old CLR Object. It's a simple C# class with public properties and no framework dependencies — the standard way to model JSON data in .NET. When you call JsonConvert.DeserializeObject<MyClass>(json) (Newtonsoft) or JsonSerializer.Deserialize<MyClass>(json) (System.Text.Json), you're deserialising into a POCO.

The Boilerplate Problem

Given this JSON from a typical e-commerce API:

{
  "orderId": 10042,
  "customer": {
    "id": 7,
    "name": "Alice Johnson",
    "email": "alice@example.com"
  },
  "items": [
    { "sku": "WIDGET-001", "qty": 2, "price": 19.99 }
  ],
  "total": 39.98,
  "placedAt": "2025-06-15T14:30:00Z",
  "isPaid": true
}

Writing the C# model classes by hand — with correct types, proper PascalCase names, JSON attributes, and nullable annotations — would take 10–15 minutes. The generator does it in under a second.

Using the Generator

  1. Paste your JSON into the input panel
  2. Set the Root class name (e.g. OrderResponse)
  3. Toggle your preferred options using the switches
  4. Copy the generated C# output

Available Options

OptionWhat it does
NamespaceWraps all classes in a namespace GeneratedModels { } block
JsonProperty attrsAdds [JsonProperty("camelCaseName")] so Newtonsoft maps correctly
Nullable typesAppends ? to value types (e.g. int?, bool?) for nullable safety
XML docsAdds /// <summary> comments to each class

Understanding the Generated Output

For the order JSON above with all options enabled, the generator produces:

using Newtonsoft.Json;

namespace GeneratedModels
{
    /// <summary>Represents a OrderResponse object.</summary>
    public class OrderResponse
    {
        [JsonProperty("orderId")]
        public int OrderId { get; set; }

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

        [JsonProperty("items")]
        public List<Items> Items { get; set; }

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

        [JsonProperty("placedAt")]
        public DateTime PlacedAt { get; set; }

        [JsonProperty("isPaid")]
        public bool IsPaid { get; set; }
    }

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

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

        [JsonProperty("email")]
        public string Email { get; set; }
    }
}
💡
Type Inference
The generator automatically detects DateTime from ISO 8601 strings, int vs double from numbers, and generates List<T> for arrays. No manual type assignment needed.

PascalCase Conversion

JSON properties are almost always camelCase while C# properties use PascalCase by convention. The generator handles this automatically: orderIdOrderId, firstNameFirstName, is_activeIsActive.

The [JsonProperty] attribute preserves the original JSON name for serialisation, so round-trip accuracy is guaranteed.

Nested Objects and Arrays

The generator recurses into nested objects and arrays of objects, producing a separate class for each unique object shape. Nested objects become named classes based on their property key — "customer": {...} generates a Customer class, "items": [{...}] generates an Items class and types the property as List<Items>.

ℹ️
Heterogeneous Arrays
If an array contains mixed types (e.g. strings and numbers), the generator defaults to List<object>. For best results, ensure your sample JSON has representative values in each array.

Adapting to System.Text.Json

The generator outputs Newtonsoft-style [JsonProperty] attributes. If you're using System.Text.Json (the modern .NET default), swap the attribute:

// Newtonsoft.Json
[JsonProperty("orderId")]
public int OrderId { get; set; }

// System.Text.Json
[JsonPropertyName("orderId")]
public int OrderId { get; set; }

Best Practices After Generating

  • Review all object types — these are the generator's way of saying "I couldn't infer this." Replace them with the correct type once you know what the field contains.
  • Enable nullable reference types in your .csproj (<Nullable>enable</Nullable>) and add ? to properties that can be null.
  • Rename Items classes to be more descriptive — ItemsOrderLineItem makes the model self-documenting.
  • Consider records for immutable models in C# 9+: change public class Foo { get; set; } to public record Foo(...);.
⚠️
Sample Data Matters
The generator infers types from the values in your sample JSON. If a field is null in your sample but is actually a string in real responses, the generator will type it as object. Use a representative payload with populated values for best results.

Recommended Workflow

  1. Make the API call in your browser's DevTools or Postman and copy the raw response body
  2. Paste into the JSON → C# tool and set the root class name to match the response concept
  3. Enable Namespace, JsonProperty attrs, and Nullable types
  4. Copy the output and create a new Models/ file in your project
  5. Rename generic class names (ItemsProduct) for clarity
C#
Open in Dev Cosmos
JSON → C# Class Generator →
More from the Blog