Using Json.NET converters to deserialize properties
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Json.NET converters are the right tool when a JSON value does not map cleanly to the target C# property type. Instead of spreading special-case parsing logic across your model classes, you can put the custom read behavior in one converter and attach it either to a property or to a type.
Why a Converter Is Useful
Default deserialization works well when JSON and C# shapes already match. It breaks down when:
- a property can arrive as more than one JSON type
- a string must be converted into a richer domain type
- one property needs special parsing rules without changing the whole model
For example, imagine a field that is sometimes an integer and sometimes a quoted string:
or:
A custom converter lets one C# property handle both forms predictably.
A Property-Level Converter Example
Here is a converter that accepts either an integer token or a numeric string and always returns an int.
Attach it directly to the property:
Now both JSON forms deserialize cleanly into Id.
Deserializing the Property
This is the most common Json.NET converter pattern: a property has a weird input contract, so the converter absorbs that inconsistency.
Property-Level vs Type-Level Registration
You can register converters in three common places:
- directly on the property with
[JsonConverter(...)] - on the target type itself
- globally in
JsonSerializerSettings
Property-level registration is the safest default when only one field needs special handling. Global registration is useful only when the behavior should apply everywhere the type appears.
Global converters are powerful, but they also make behavior more implicit. If another model later uses the same type with different expectations, global registration can become surprising.
Work With JToken When the Shape Is Complex
For more complex JSON, reading into a JToken first can make the converter clearer.
This approach is easier when the incoming JSON shape varies more than the target C# type does.
Common Pitfalls
- Registering a converter globally when only one property actually needs special handling.
- Silently accepting malformed JSON and hiding real data-quality issues.
- Forgetting to implement
WriteJsonconsistently when the object must also be serialized back out. - Using a converter when a simple
[JsonProperty]name mapping would have solved the problem. - Writing property-specific logic in a general-purpose converter and making later reuse confusing.
Summary
- Json.NET converters are for properties or types that need custom read or write behavior.
- Property-level converters are usually the clearest and safest option.
- A converter can normalize multiple JSON token shapes into one C# property type.
- '
JTokenis useful when the incoming JSON shape varies significantly.' - Keep the converter narrow, explicit, and consistent with the domain rules it enforces.

