How to convert JSON to Object in C# Examples|Serialize and deserialize

JSON is abbreviated as javascript object notation, which stores text-based keys and values.

Let us first understand what is JSON type. It is a text-based file format that is used to save and transfer data between software applications. Usually, the browser sends the request data to the server and receives the response data in JSON format.

Let’s declare a JSON object like this.

{
  "name": "Tom",
  "id": "1",
  "age": 25
}

Let’s define the C# class for holding the json data,

Create properties and fields as per the JSON structure of a json object.

public class Employee
{
    public string name { get; set; }
    public string id { get; set; }
    public integer age { get; set; }
}

Convert JSON to C# Object class

In multiple ways, we can convert a JSOn string to C# Class objects.

  • use Newtonsoft.Json package

  • First, Install Newtonsoft packages with the latest version using the NuGet package manager in your project

Install-Package Newtonsoft.Json

or use the below code

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
  </ItemGroup>

run `dotnet restore command

  • Import Newtonsoft.Json module into your code

    using Newtonsoft.Json;
    
  • Create a class with the same properties as per the JSON structure

  • DeserializeObject in JsonConvert takes JSOn object as input and converts into Class

  • You can access properties using class name.property

Here is an example

using System;
using Newtonsoft.Json;

public class Employee {

  public string name {
    get;
    set;
  }
  public string id {
    get;
    set;
  }
  public int age {
    get;
    set;
  }
}
public class Program {
  public static void Main() {
    string jsonString = @"{""name"": ""Tom"",""id"": ""1"",""age"": 25}";
    Console.WriteLine(jsonString);

    // πŸ‘‡  Convert JSON String to an Object

    var employee = Newtonsoft.Json.JsonConvert.DeserializeObject < dynamic > (jsonString);

    Console.WriteLine(" πŸ‘‡ Printing an Object properties  πŸ‘‡");

    Console.WriteLine(employee.name);
    Console.WriteLine(employee.age);
    Console.WriteLine(employee.id);

  }
}

Output:

{"name": "Tom","id": "1","age": 25}
πŸ‘‡ SortedDictionary keys ascending Defaul πŸ‘‡
Tom
25
1

How to convert Class Object to JSON in C#

Newtonsoftprovides Newtonsoft.Json.JsonConvert.SerializeObject object that takes an object and returns a json object. It returns a simple json object without format.

if Newtonsoft.Json.Formatting.Indented property is passed, It returns a JSON string in indented prettyprint style

using System;
using Newtonsoft.Json;

public class Employee {

  public string name {
    get;
    set;
  }
  public string id {
    get;
    set;
  }
  public int age {
    get;
    set;
  }
}
public class Program {
  public static void Main() {
    string jsonString = @"{""name"": ""Tom"",""id"": ""1"",""age"": 25}";

    // πŸ‘‡  Convert JSON String to an Object

    var employee = Newtonsoft.Json.JsonConvert.DeserializeObject < dynamic > (jsonString);

    // πŸ‘‡  Convert Object to JSON String to an Object

    string json = Newtonsoft.Json.JsonConvert.SerializeObject(employee);
    string jsonFormatted = Newtonsoft.Json.JsonConvert.SerializeObject(employee, Newtonsoft.Json.Formatting.Indented);
    Console.WriteLine(" πŸ‘‡ Printing an JSON object with or without formatting  πŸ‘‡");

    Console.WriteLine(json);
    Console.WriteLine(jsonFormatted);

  }
}

Output:

πŸ‘‡ Printing an JSON object with or without formatting πŸ‘‡
{"name":"Tom","id":"1","age":25}
{
"name": "Tom",
"id": "1",
"age": 25
}