Convert JSON into C# classes for System.Text.Json. Property names are turned into PascalCase, and every property carries a [JsonPropertyName] attribute so it binds to the exact original key.
It saves the copy-and-type work for ASP.NET Core request and response models, types for HttpClient.GetFromJsonAsync<T>() calls to external APIs, and data classes in Unity or MAUI apps.
Type name (for example PaymentResult)..cs file and add your namespace.JsonSerializer.Deserialize<PaymentResult>(json) or GetFromJsonAsync<PaymentResult>(url).{
"paymentId": "pay_8f2k",
"amount": 15000,
"currency": "KRW",
"approved": true,
"approvedAt": "2024-05-01T12:30:00Z",
"card": { "issuer": "VISA", "last4": "4242" },
"metadata": null
}using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class PaymentResult
{
[JsonPropertyName("paymentId")]
public string PaymentId { get; set; } = string.Empty;
[JsonPropertyName("amount")]
public int Amount { get; set; }
[JsonPropertyName("currency")]
public string Currency { get; set; } = string.Empty;
[JsonPropertyName("approved")]
public bool Approved { get; set; }
[JsonPropertyName("approvedAt")]
public string ApprovedAt { get; set; } = string.Empty;
[JsonPropertyName("card")]
public Card Card { get; set; } = new();
[JsonPropertyName("metadata")]
public JsonElement? Metadata { get; set; }
}
public class Card
{
[JsonPropertyName("issuer")]
public string Issuer { get; set; } = string.Empty;
[JsonPropertyName("last4")]
public string Last4 { get; set; } = string.Empty;
}
Every property has [JsonPropertyName], so paymentId ↔ PaymentId matches regardless of casing rules. metadata is only ever null, so its type is unknown and it becomes JsonElement?. String and object properties get string.Empty / new() initializers so the code compiles without nullable warnings.
[JsonPropertyName("originalKey")] on each property, with using System.Text.Json.Serialization;.string, int, long for integers beyond 32 bits, double for decimals, bool, and List<T> for arrays.JsonElement, adding using System.Text.Json;.null become nullable (int?, string?).= string.Empty; or = new(); initializers to avoid nullable reference warning CS8618.Value suffix (e.g. RootValue).// Root: List<RootItem> comment and the element class.Convert a sample response from a payment or shipment-tracking API and use it as the type in await http.GetFromJsonAsync<PaymentResult>(url), instead of pulling fields one by one out of a JsonDocument.
Generate classes from the payload a third-party service posts to you and accept them as a [FromBody] parameter, so model binding and validation work as usual.
Pinning the structure of a JSON data file read by a Unity or console app to classes means a changed data file surfaces as a deserialization error right away.
[JsonProperty]) output is not generated. If you use Json.NET, switch the attributes to [JsonProperty("key")].string. For ISO 8601 values, changing the type to DateTime or DateTimeOffset is enough for System.Text.Json to parse them.new() and string? assume C# 9+ with the nullable context enabled (the default since .NET 6).record or required output. For immutable models, change { get; set; } to { get; init; }.[JsonPropertyName] even when the key already matches?System.Text.Json is case-sensitive by default. In console or worker projects without custom JsonSerializerOptions, paymentId would not bind to PaymentId, so the attribute is always written to work regardless of configuration.
JsonElement? property?Check ValueKind, then use GetString(), GetInt32() or Deserialize<T>(). Once you know the real type, replacing JsonElement with it is the cleaner option.
The output follows the C# PascalCase convention. The original key lives in [JsonPropertyName], so you can rename properties freely as long as the attribute stays.
JsonUtility only handles public fields of [Serializable] classes, not properties, and ignores [JsonPropertyName]. In Unity, pair the output with the Newtonsoft.Json package or System.Text.Json instead.