knowledgecenter-breadcrum

Knowledge Center

22 Feb, 2023

Serialization/Deserialization of JSON objects using Newtonsoft.Json in C#

Posted on 22 Feb, 2023 by Admin, Posted in Dynamics 365 C# Power Platform

Serialization/Deserialization of JSON objects using Newtonsoft.Json in C# Blogs

Serialization/Deserialization of JSON objects using Newtonsoft.Json in C#

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

  • A collection of name/value pairs.
  • An ordered list of values.

NOTE: The approach described below can also be used for plugins in Dynamics 365 Customer Engagement (CE).

Below is a sample JSON:

Context: We will be working with C# code in Visual Studio 2019

The WRONG way to write json in code is by creating a json string manually:

Above json code at first glance seems fine but once json has hundreds of embedded objects the code will neither be readable nor maintainable.

The CORRECT way to write json is by using classes.

Here we will be using Newtonsoft.Json.

Firstly, we import Newtonsoft.Json into our solution.

1. In the Solutions Explorer window, right click the solution and select “Manage NuGet Packages for Solution”.

2. In the Browse Tab, Search for Newtonsoft.Json and install the latest version.

Once Newtonsoft.Json is installed we can start using it in code.

We start by importing Newtonsoft.Json with “using” keyword.

using Newtonsoft.Json

Now we create a class file in the same project.

1.  In the Solutions Explorer window, right click the project and select Add -> New Item…

2. Select C# Class file and name it with .cs extension and click on Add.

A new class file will be created with auto-generated code. Remove the class LeadJson{}.

Now to add the contents to this class file.

Copy the JSON.

Go to the newly created class file and place cursor between namespace JsonTesting{} curly brackets.

Now go to Edit tab on menu bar. Select Paste Special and then select Paste JSON as Classes.

The classes will be automatically created based on the json structure.

We will change the name of Rootobject class to LeadJson and Datum to LeadFields.

Also, we will be using Lists instead of arrays []. So Datum[] will become List and string[] becomes List.

So finally, LeadJson class file will look as below:

Another way to generate JSON classes is to use online tools.

Here we will be using a site called json2csharp.com. Paste the sample json on the left side and click convert.

Copy the right-side code and paste it inside the newly created json class file.

Below I have renamed Datum as LeadFields, List to List and Root as LeadJson.

Now the class is ready to be used. Save it and go back to the main class (here Lead.cs).

Serialization

We create a function that serializes the LeadJson class into a string and returns it.

We start by creating an object of Account class and assigning it in LeadFields class.

Since the “data” object is a list, We will use Add method to add an item to a List.

We create the data and the trigger List class objects and assign both in the LeadJson class.

Finally, we use JsonConvert.SerializeObject() method to pass leadJson object and the parameter to format the json to get the indented json.

Deserialization

Now to reverse the serialization i.e., to convert json string to json objects we do Deserialization.

We create a DeserializeJson function and pass the json string as the parameter.

Note: Use whichever logging method available to you to log the data. Here I’ve used TraceWriter.

We use JsonConvert.DeserializeObject() method to get LeadJson root object.

Since all the leads are in the “data” List object. We use foreach to cycle through each individual lead.

Accessing the lead data is as easy as accessing the objects. We simply use lead.[Field Name] to get the required data.

Thank you for reading our blog.

Comment

This is a Required Field

Loading

Recent Updates

Thumbnail_top 10 benefits of
Blogs
02 Dec, 2024

Top 10 Benefits of Microsoft Dynamics 365

This blog lists the advantages of Microsoft Dynamics 365, focusing on how it can improve your business operations.    …

READ MORE
Thumbnail_Microsoft365 Vs D365
Blogs
12 Nov, 2024

Microsoft 365 vs Dynamics 365

This blog will help you understand the key difference between Microsoft 365 and Dynamics 365, so you can make an…

READ MORE
Thumbnail_What is Microsoft Dynamics 365
Blogs
05 Nov, 2024

What is Microsoft Dynamics 365?

This blog explores Microsoft Dynamics 365, a cloud solution for handling different business activities smoothly. The rising trends of competition…

READ MORE