How do I turn a C# object into a JSON string in .NET?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Turning a C# object into a JSON string is serialization: you take an in-memory object graph and convert it into text that can be logged, sent over HTTP, or written to a file. In modern .NET, the normal built-in tool is System.Text.Json, although Newtonsoft.Json is still common in older codebases and some advanced scenarios.
Serialize With System.Text.Json
For new .NET code, the direct answer is JsonSerializer.Serialize.
That produces a compact JSON string. For many API and logging scenarios, this is enough.
Control The Output With Serializer Options
Real applications often need more than the compact default. JsonSerializerOptions lets you control indentation, property naming, and null handling.
This is the usual place to make JSON output match the conventions of an HTTP API or frontend client.
Collections And Nested Objects Work Naturally
Serialization is not limited to one flat object. Lists and nested objects are handled automatically as long as the contained types are serializable.
This is one reason JSON serialization is so useful for APIs: your object structure maps naturally into JSON objects and arrays.
When Newtonsoft.Json Is Still Useful
Older .NET applications often use Json.NET, and some teams still prefer it for compatibility or specialized features. The equivalent basic call is:
Indented output looks like this:
If you are working in an existing codebase that already uses Json.NET pervasively, consistency may matter more than switching every serializer call immediately.
Think About Serialization Boundaries
The basic API call is easy, but the real engineering question is which object you should serialize. Dumping a full ORM entity or a complicated domain object graph directly into JSON can expose fields you did not intend to publish.
In public APIs, it is often better to serialize a DTO designed for the response contract rather than whatever entity type happens to be available. The JSON string should reflect the data contract, not accidental internal structure.
Common Pitfalls
- Serializing the wrong object type and exposing fields that should stay internal.
- Expecting JSON property names to use camelCase without configuring the serializer.
- Forgetting that cycles and unsupported members can cause serialization failures.
- Mixing
System.Text.JsonandNewtonsoft.Jsonconventions in the same API without noticing behavioral differences. - Treating serialization as a formatting concern only, when it is really part of the application's external contract.
Summary
- In modern .NET, use
JsonSerializer.SerializefromSystem.Text.Jsonto turn a C# object into JSON text. - Add
JsonSerializerOptionswhen you need indentation, camelCase names, or null-handling rules. - Nested objects and collections serialize naturally when the types are supported.
- '
Newtonsoft.Jsonremains valid in older or feature-rich codebases, but the core idea is the same: serialize the right object deliberately.'

