---
title: "How to convert an Object to JSON in C#?"  
description: "How to convert an Object to JSON in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/forum/160702/how-to-convert-an-object-to-json-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# How to convert an Object to JSON in C#?

How to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) an Object to JSON in C#?

## Replies

### Reply by Ravi Vishwakarma

You can convert an **object to JSON** using the built-in `System.Text.Json` or `Newtonsoft.Json` **(also known as JSON.NET)** libraries. Both libraries provide methods to serialize C# objects into [**JSON**](https://www.mindstick.com/blog/304/introduction-to-json) format.

## Here's how you can do it using both libraries:

## Using ‘System.Text.JSON’

```cs
using System.Text.JSON;
using System;
namespace ConsoleApplication1
{
   public class ObjectToJSON
   {
       public static void Main(string[] args)
       {
           ObjectToJSON.Convert();
           Console.ReadLine();
       }
       public static string Convert()
       {
           // Define an object
           var obj = new
           {
               Name = "John",
               Age = 30,
               IsStudent = true
           };
           // Serialize the object to JSON
           string json = JsonSerializer.Serialize(obj);
           // Output the JSON
           Console.WriteLine(json);
           return json;
       }
   }
}
```

## Using ‘Newtonsoft.Json’

```cs
using Newtonsoft.Json;
using System;
namespace ConsoleApplication1
{
   public class ObjectToJSON
   {
       public static void Main(string[] args)
       {
           ObjectToJSON.Convert();
           Console.ReadLine();
       }
       public static string Convert()
       {
           // Define an object
           var obj = new
           {
               Name = "John",
               Age = 30,
               IsStudent = true
           };
           // Serialize the object to JSON
           string json = JsonConvert.SerializeObject(obj);
           // Output the JSON
           Console.WriteLine(json);
           return json;
       }
   }
}
```

\
**Explanation**

1. In both examples, an anonymous type is used to define the object to be serialized. You can change it with any custom class or object you want to convert to JSON.
2. `JsonSerializer.Serialize` method (from `System.Text.Json`) or `JsonConvert.SerializeObject` method (from `Newtonsoft.Json`) is used to convert the object to JSON format.
3. The resulting JSON string is then printed to the console.

\
**Library Selection**

1. `System.Text.Json` is part of the .NET Core and .NET 5+ frameworks, and it provides high-performance JSON parsing and serialization.
2. `Newtonsoft.Json` (**JSON.NET**) is a popular third-party library with extensive features and flexibility. It has been widely used in the .NET ecosystem for many years and provides excellent support for various scenarios.


---

Original Source: https://www.mindstick.com/forum/160702/how-to-convert-an-object-to-json-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
