JSON Input

C# Output

What does the JSON to C# converter generate?

If you've ever typed out twenty properties to match an API response, this is for you. Paste a JSON sample and you get one C# class per object, with PascalCase properties and a [JsonPropertyName("subscriberId")] attribute on each one. The attribute matters: System.Text.Json matches names case-sensitively by default, so without it a camelCase payload wouldn't bind to PascalCase properties unless you change the serializer options.

Arrays become List<T> (nested arrays become List<List<T>>), nested objects become their own classes, and every element of an array is inspected, so keys that only some items have still show up. Whole numbers are int, or long / ulong once a value no longer fits in 32 bits, and decimal past that. If two different objects share a key name, such as a top-level address and billing.address, the second class is named BillingAddress and the property points at it. An empty object becomes Dictionary<string, object>.

With Nullable Types on, fields that were null or missing in the sample get a ? and the file starts with #nullable enable. Turn Use Properties off if you want public fields instead of { get; set; }. Values that were only ever null come out as object?, so give those a real type once you know it. Everything runs in your browser; the JSON never leaves the page.

How to use it

The output updates a moment after you stop typing, so there's no Convert button.

1

Paste JSON or upload a file

Paste into the JSON Input panel, click Upload for a .json or .txt file, or click Sample. For example:

{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}

Set Class Name for the root class. If the JSON root is an array of objects, the item class gets that name and a comment shows the List<RootObject> to deserialize into.

2

Read the classes

The C# Output panel starts with the using lines it needs, then the classes, with nested ones first. The example above gives public string SubscriberId { get; set; }, public double DataUsage { get; set; } and so on, each with its JSON name in an attribute.

3

Copy or download

Copy puts the code on your clipboard and Download saves a .cs file. Then deserialize with JsonSerializer.Deserialize<RootObject>(json).

Where this saves time

Typed HttpClient calls

Grab a response from the API you're integrating with, generate the model, and call GetFromJsonAsync<Order>() instead of picking values out of a JsonDocument.

Webhook payloads

Payment and shipping providers post JSON with dozens of fields and odd key names like event-type. The attribute keeps the original key, so the property can still be called EventType.

Options classes for appsettings.json

Paste a section of your config and get a class to bind with the options pattern. Configuration binding matches property names case-insensitively and ignores the attributes, so they do no harm there.

Test fixtures

Turn a captured JSON fixture into classes so your tests can assert on order.Customer.Email instead of string-matching JSON.

Common questions

Does this work with Newtonsoft.Json?

Mostly. Newtonsoft.Json matches property names case-insensitively, so the classes deserialize fine for ordinary camelCase keys, but it ignores [JsonPropertyName]. For keys like first name or user-id, swap the attribute for Newtonsoft's [JsonProperty("user-id")].

Why does a property end in Value?

C# doesn't allow a member with the same name as its class (error CS0542). So a key address inside a class called Address becomes AddressValue; the attribute still maps it to "address". Keys that start with a digit, like 2fa, get a leading underscore.

How are big numbers typed?

The page reads integers without rounding them, then picks int, long, ulong or decimal depending on the largest value it sees. An ICCID like 8901240544102066246 gets long.

What if the root isn't an object?

For null, a string, a number, an empty array or an array of plain values there's nothing to turn into a class, so the output panel explains that instead of showing empty code.

Do I need C# 8 for the nullable output?

Yes, #nullable enable and nullable reference types (string?) need C# 8 or later. Turn Nullable Types off for older projects. You may still see CS8618 warnings for non-nullable properties without initializers; add required or default values as suits your code.

Is my JSON uploaded?

No. The classes are generated by JavaScript in this page, so nothing is sent to a server. Input is capped at 256 KB.

Other Code Generation Tools

Need the same payload in another language, or want to validate it first?