csharp json dotnet api

C# Class to JSON: Generate Sample Payloads from Your Models

How to turn C# classes into example JSON payloads for API docs, tests, and integration work — without running any code.

· ByteKiln

You’ve written a C# model and you need to show a sample JSON payload for an API endpoint. Writing it by hand works, but it’s tedious — especially when the class has ten nested types. This is the problem the C# to JSON converter solves.


What It Does

The tool takes a C# class definition (or multiple class definitions) and generates a representative JSON object showing what a serialized instance would look like. It:

  • Infers sensible placeholder values based on property types
  • Expands nested class references into objects
  • Turns List<T>, IEnumerable<T>, and T[] into sample arrays
  • Handles Dictionary<string, T> as JSON objects
  • Maps Guid to a UUID-format string
  • Maps DateTime / DateTimeOffset to an ISO 8601 timestamp string
  • Handles nullable properties (int?, string?) with null or default values

A Quick Example

Input:

public class Order
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public DateTime CreatedAt { get; set; }
    public bool IsPaid { get; set; }
    public List<OrderItem> Items { get; set; }
}

public class OrderItem
{
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

Output:

{
  "Id": 0,
  "CustomerName": "string",
  "CreatedAt": "2024-01-01T00:00:00Z",
  "IsPaid": false,
  "Items": [
    {
      "ProductName": "string",
      "Quantity": 0,
      "Price": 0.0
    }
  ]
}

The nested OrderItem is expanded into a sample object inside the Items array. You get a structural representation of the full model in one paste.


When to Use This vs. Actually Running Code

The tool generates a static structural example — it doesn’t run the code, doesn’t invoke constructors, and doesn’t call factory methods. This makes it useful in situations where you don’t have a running application:

  • Writing API documentation before implementation
  • Creating test fixtures for an endpoint you’re designing
  • Sharing a payload example with a frontend developer
  • Onboarding someone to an API they haven’t worked with before
  • Generating mock data for integration tests

If your class has complex initialization logic, custom constructors, or computed properties, the generated output shows the property structure, not the runtime state. That’s usually exactly what you want for documentation and testing.


Handling Complex Types

Dictionaries

public class Config
{
    public Dictionary<string, string> Settings { get; set; }
}

Becomes:

{
  "Settings": {
    "key": "value"
  }
}

A dictionary maps naturally to a JSON object. The converter shows a single representative key-value pair.

Generics and collections

public class PagedResult<T>
{
    public List<T> Items { get; set; }
    public int TotalCount { get; set; }
    public int Page { get; set; }
}

For generic type parameters, the tool outputs the generic type name as a placeholder object. If you paste in a concrete class, the expansion is fully realized.

Nullable types

public class Profile
{
    public string Name { get; set; }
    public string? Bio { get; set; }
    public int? Age { get; set; }
}

Becomes:

{
  "Name": "string",
  "Bio": null,
  "Age": null
}

Nullable value types and nullable reference types both produce null in the example.


Using the Output for API Documentation

The generated JSON is a solid starting point for OpenAPI/Swagger example values. Take the output and either:

  1. Paste it directly as an example in your Swagger UI example block
  2. Use it as a Postman body template
  3. Put it in your README as a request/response example

The structure is correct by definition — it matches your C# model. You only need to swap in realistic values (real names, valid dates, etc.) if the documentation is customer-facing.


Using the Output for Tests

For unit and integration tests, the generated JSON is a useful starting point for test fixtures:

{
  "Id": 1,
  "CustomerName": "Alice",
  "CreatedAt": "2026-01-15T09:00:00Z",
  "IsPaid": true,
  "Items": [
    {
      "ProductName": "Keyboard",
      "Quantity": 1,
      "Price": 79.99
    }
  ]
}

Change the placeholder values to valid test data, and you have a fixture you can serialize back with System.Text.Json.JsonSerializer.Deserialize<Order>(json) or load from a file in an xUnit [Fact].


Known Limitations

This is a static analysis tool, not a runtime serializer. That means:

  • No constructor logic — if your class has a non-trivial constructor that sets property defaults, those won’t be reflected in the output.
  • No custom JsonConverter support — if you have a custom converter that changes how a type serializes, the output reflects the property structure, not the converter’s output.
  • No inheritance chain — virtual or abstract base class properties require the base class to also be in the input.
  • No annotations[JsonPropertyName], [JsonIgnore], [JsonConverter] attributes are not processed. Property names in the output match the C# identifier.

For these cases, the tool is still useful as a structural starting point. Copy the output, adjust the field names, and you have a correct payload.


Tips

Include all referenced classes. Paste the full set of models that the root class depends on. The converter can only expand types it can see in the input.

Use it to understand unfamiliar code. If you’re new to a codebase and see a complex model hierarchy, paste all the classes in and look at the JSON output. It’s often faster than reading the code to understand the shape of the data.

Combine with the JSON Formatter. If you want to compact the output for a curl example or query string, paste the generated JSON into the JSON Formatter and use “copy compact”.


If you’re going in the other direction — turning a JSON API response into C# model classes — the JSON to C# guide covers that workflow. And if you need the database side of the picture too, the C# to SQL guide generates CREATE TABLE statements from the same entity classes.

The C# to JSON converter is most valuable at the boundaries: when you’re working between systems, writing documentation, or setting up tests. It takes the structural work out of the picture so you can focus on what the data actually needs to contain.