Serializing and Deserializing Json objects with key value pairs in C#

Posted On March 30, 2022 by Shruti Gupta Posted in  Tagged in ,

If we have a json data whose objects have one or more key value pairs also known as properties and we need to separate them into individual objects then Serializing and Deserializing method can be used in C#.

Sample Json data :

     {

                                “FirstName”:”Aditya”,

                                “MiddleName”:”Ashok”,

                                “LastName”:”Somwanshi”,

                                “Phone”:[“9004802526″,”34304235”],

                                “Address”:{“Primary”:”Panvel”, “Secondary”:”Cloudfronts”}

                            }

Desired Output:

{

  “FullName”: “Aditya Ashok Somwanshi”,

  “Primary Phone”: “9004802526”,

  “Secondary Phone”: “34304235”,

  “Primary Address”: “Panvel”,

  “Secondary Address”: “Cloudfronts”

}

  • First open Visual Studio and create new project in Console application template.
  • Give your Project a name and click next, you will be directed to a new page where you can start with your code.
  • Now we need to install some packages into our project to use it in our code. So at top in the tools section select NuGet Package Manager and in that select Manage Nuget Packages for Solution
  • Now search for Newtonsoft.Json and install the package

The purpose for installing this package is to access serialize and deserialize functions in our code.

  • To add a class to main program you need to right click on your project and then select and choose add new item

Select class and name the class and add.

using System.Collections.Generic;

using Newtonsoft.Json;

namespace Human

{

    public class Address

    {

        public string Primary { get; set; }

        public string Secondary { get; set; }

    }

    public class Person

    {

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }

        public List<string> Phone { get; set; }

        public Address Address { get; set; }

    }

    public class Root

    {

        public string FullName { get; set; }

        [JsonProperty(“Primary Phone”)]

        public string PrimaryPhone { get; set; }

        [JsonProperty(“Secondary Phone”)]

        public string SecondaryPhone { get; set; }

        [JsonProperty(“Primary Address”)]

        public string PrimaryAddress { get; set; }

        [JsonProperty(“Secondary Address”)]

        public string SecondaryAddress { get; set; }

    }

}

  • Now the main program

using Newtonsoft.Json;

using System;

namespace Human

{

    public class Program

    {

        public static void Main(string[] args)

        {

            string json = @”{

                                “”FirstName””:””Aditya””,

                                “”MiddleName””:””Ashok””,

                                “”LastName””:””Somwanshi””,

                                “”Phone””:[“”9004802526″”,””34304235″”],

                                “”Address””:{“”Primary””:””Panvel””, “”Secondary””:””Cloudfronts””}

                            }”;//Defining Json

            Person human = JsonConvert.DeserializeObject<Person>(json);//Deserializing Json

            Root rt = new Root();

            rt.FullName = human.FirstName + ” ” + human.MiddleName + ” ” + human.LastName;  //Concatenating Names

            rt.PrimaryPhone = human.Phone[0];

            rt.SecondaryPhone = human.Phone[1];

            rt.PrimaryAddress = human.Address.Primary;

            rt.SecondaryAddress = human.Address.Secondary;

            string output = JsonConvert.SerializeObject(rt, Formatting.Indented);//Serializing Json

            Console.WriteLine(output);

        }

    }

}

  • OUTPUT:

Share Story :