Convert object of any type to JObject with Json.NET
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
JObject is useful when you need dynamic JSON manipulation after taking an ordinary .NET object and turning it into a token tree. The straightforward call is JObject.FromObject, but a robust solution also needs to handle nulls, existing JToken values, serializer settings, and cases where the root value is not actually an object.
The Normal Case: JObject.FromObject
For plain classes or anonymous objects, Json.NET already gives you the core API.
This works because the root value is object-shaped. Json.NET reflects over the properties and creates a JObject tree.
A Reusable Helper Method
In real code, conversion logic is cleaner when centralized.
This gives you a single entry point and avoids double-converting token types that are already JSON-aware.
Why Existing JToken Values Need Special Handling
JObject.FromObject is meant for CLR objects, not for every possible JSON token shape. If the caller passes a JArray, a primitive JValue, or some other JToken, you need to decide what object shape should result.
That is why the helper above wraps non-object tokens under a value property.
Whether wrapping is the right choice depends on your contract. The key point is that arrays and primitives are not JObject roots by themselves.
Serializer Settings Matter
FromObject uses serializer rules. If naming strategy, null handling, enum formatting, or converters matter to your API, create the serializer explicitly.
With those settings, MiddleName is omitted and property names become camelCase.
Dynamic Mutation After Conversion
The main reason people choose JObject over ordinary serialization is that they want to inspect or alter the JSON dynamically.
This is a good fit for integration layers, gateways, logging pipelines, or schema-translation steps.
When JObject Is the Wrong Tool
If the JSON shape is fixed and well-known, a strongly typed DTO is usually better. JObject is convenient, but it moves errors from compile time to runtime.
Use JObject when:
- the payload shape is partially dynamic
- you are transforming third-party JSON
- you need selective inspection without defining many small DTOs
Do not use it by default for ordinary application-domain models if strong types would be clearer.
Common Pitfalls
The biggest mistake is assuming every value can become a JObject directly. Arrays, primitives, and nulls need special treatment.
Another mistake is forgetting that serializer settings affect the resulting JSON. Two services using different settings can produce different property names or null behavior from the same CLR object.
A third issue is mutating nested properties without null checks. SelectToken or index access can return null, and dynamic JSON code should handle that deliberately.
Finally, overusing JObject can make code harder to maintain than simply defining a proper model class.
Summary
- '
JObject.FromObjectis the standard Json.NET way to convert object-shaped values.' - Wrap conversion in a helper if you need null and
JTokenhandling in one place. - Existing
JObjectvalues should usually be reused, not reserialized. - Serializer settings control naming, null handling, and other output details.
- '
JObjectis ideal for dynamic transformation, not for every JSON use case.' - Treat array roots and primitive roots explicitly instead of assuming they are objects.

