How to Convert an object to JSON in C#
By FoxLearn 2/5/2025 7:05:39 AM 124
You can easily perform serialization using one of two main options:
- Use
JsonSerializer.Serialize()
(fromSystem.Text.Json
). - Use
JsonConvert.SerializeObject()
(fromNewtonsoft.Json
).
Below, I'll walk through examples using both methods.
Using JsonSerializer.Serialize() (from System.Text.Json)
For example, how you can serialize an object with System.Text.Json
using the JsonSerializer.Serialize()
method:
using System.Text.Json; var book = new Book() { Title = "1984", Author = "George Orwell", Pages = 328, PublishDate = new DateTime(year: 1949, month: 6, day: 8), IsBestseller = true }; var json = JsonSerializer.Serialize(book);
In this example, the Book
object is serialized into a JSON string using the default settings. The output would look like this:
{"Title":"1984","Author":"George Orwell","Pages":328,"PublishDate":"1949-06-08T00:00:00","IsBestseller":true}
Customizing Serialization with JsonSerializerOptions
You can modify the default behavior of serialization by passing in a JsonSerializerOptions
object.
For example, here’s how to pretty-print the JSON:
var options = new JsonSerializerOptions() { WriteIndented = true }; var json = JsonSerializer.Serialize(book, options);
This will output the JSON in a more readable format:
{ "Title": "1984", "Author": "George Orwell", "Pages": 328, "PublishDate": "1949-06-08T00:00:00", "IsBestseller": true }
Using JsonConvert.SerializeObject() (from Newtonsoft.Json)
If you prefer to use the Newtonsoft.Json
package, you first need to install it via NuGet:
Install-Package Newtonsoft.Json
Once installed, here’s how to serialize an object with JsonConvert.SerializeObject()
:
using Newtonsoft.Json; var book = new Book() { Title = "1984", Author = "George Orwell", Pages = 328, PublishDate = new DateTime(year: 1949, month: 6, day: 8), IsBestseller = true }; var json = JsonConvert.SerializeObject(book, Formatting.Indented);
This will produce the following JSON output:
{ "Title": "1984", "Author": "George Orwell", "Pages": 328, "PublishDate": "1949-06-08T00:00:00", "IsBestseller": true }
Notice the use of Formatting.Indented
to pretty-print the JSON.
Customizing Serialization with JsonSerializerSettings
To further customize the serialization, you can pass a JsonSerializerSettings
object. For instance, to convert property names to camel case while also pretty-printing:
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; var settings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; var person = new Person() { FirstName = "John", LastName = "Doe" }; var json = JsonConvert.SerializeObject(person, settings);
This generates the following JSON:
{ "firstName": "John", "lastName": "Doe" }
Whether you choose System.Text.Json
or Newtonsoft.Json
, both libraries provide simple and powerful ways to serialize objects to JSON. You can also customize the serialization process by using options or settings to suit your application's needs.
- Deserialize JSON using different property names in C#
- Deserialize JSON to a derived type in C#
- Deserialize JSON to a dictionary in C#
- Deserialize a JSON array to a list in C#
- Serialize a tuple to JSON in C#
- Serialize and Deserialize a Multidimensional Array in JSON using C#
- Modifying Date Format for JSON Serialization in C#
- Serialize anonymous types with System.Text.Json in C#