---
title: "How to convert JSON String to Object in C#?"  
description: "How to convert JSON String to Object in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/forum/160703/how-to-convert-json-string-to-object-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# How to convert JSON String to Object in C#?

How to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) [JSON](https://www.mindstick.com/forum/34446/convert-json-string-to-object) [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) to [Object in C#](https://www.mindstick.com/forum/161296/what-is-the-purpose-of-the-history-object-in-c-sharp)?

## Replies

### Reply by Ravi Vishwakarma

In C#, you can convert a **JSON string to an object** using the `System.Text.Json` or `Newtonsoft.Json` **(JSON.NET)** libraries.

Both libraries provide methods to deserialize JSON into C# objects.

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

## Using ‘System.Text.Json’

```cs
using System;
using System.Text.Json;
class Program
{
   static void Main()
   {
       // JSON string
       string json = "{\"Name\":\"John\",\"Age\":30,\"IsStudent\":true}";
       // Deserialize the JSON string to an object
       var obj = JsonSerializer.Deserialize<MyClass>(json);
       // Access object properties
       Console.WriteLine($"Name: {obj.Name}");
       Console.WriteLine($"Age: {obj.Age}");
       Console.WriteLine($"IsStudent: {obj.IsStudent}");
   }
   // Define a class to match the structure of the JSON object
   public class MyClass
   {
       public string Name { get; set; }
       public int Age { get; set; }
       public bool IsStudent { get; set; }
   }
}
```

\
**Using ‘Newtonsoft.Json’**

```cs
using System;
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
namespace ConsoleApplication1
{
   public class JSONToObject
   {
       public static void Convert()
       {
           // JSON string
           string json = "{\"Name\":\"John\",\"Age\":30,\"IsStudent\":true}";
           // Deserialize the JSON string to an object
           var obj = JsonConvert.DeserializeObject<MyClass>(json);
           // Access object properties
           Console.WriteLine("Name: {0}", obj.Name);
           Console.WriteLine("Age: {0}", obj.Age);
           Console.WriteLine("IsStudent: {0}", obj.IsStudent);
       }
   }
   // Define a class to match the structure of the JSON object
   public class MyClass
   {
       public string Name { get; set; }
       public int Age { get; set; }
       public bool IsStudent { get; set; }
   }
}
```

## Explanation

1. In both examples, a JSON string is provided, representing an object with properties Name, Age, and IsStudent.
2. The `JsonSerializer.Deserialize<T>` method (from `System.Text.Json`) or `JsonConvert.DeserializeObject<T>` method (from `Newtonsoft.Json`) is used to convert the **JSON string** into an instance of a specific C# class (MyClass in this case).
3. The resulting object (obj) can then be used to access its properties as usual.

## Library Selection

1. `System.Text.Json` is part of the .NET Core and .NET 5+ frameworks, providing 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/160703/how-to-convert-json-string-to-object-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
