JSON Parsing in C#

JSON
Share this

In this article, you will learn how to parse JSON in C#. JSON Parsing means to read JSON data and serialize it in .NET objects.

A lot of api’s today output the data in JSON format. To be able to use the data in your applications, you need to be able to parse the JSON data.

Step (1) : Fetch JSON Data

Create a new C# console app. Add a new folder named ‘Resources’ and add a JSON file named ‘SharePointList.json’ with the content as follows. I get the real data in my application from a REST api which reads an OnPremise SharePoint List. JSON data source in your real-world app will be something else.

Step (2) : Read JSON Data

Create the following function to read JSON from Step (1) data in a string.

Step (3) : Create Data Model

In this step, choose and create the data model as per your application’s need. You can also create a user defined class and create properties to hold the data or choose any other data type.

I have chosen my data model asDictionary<string,string>

Step (4) : Parse JSON Data

To parse JSON data, add a NuGet Package Newtonsoft.Json. Next add the namespace Newtonsoft.Json.Linq with a using statement in the console app. I will use the string from Step (2) which contains the JSON data read is a string and use this string to parse the JSON into a C# Dictionary object. The following code does the JSON parsing.

The above method named ParseJSON accepts a string object. I will pass the JSON string read in Step (2) in this method when I will call this method in the Main() method of the console app.

The first line of the method initializes a Dictionary object. The next line creates an object of JObject class of Newtonsoft.Json library named joResponse. The JSON string is passed as an input parameter to the constructor of this JObject’s Parse method. Next a for loop is initialized to traverse till the number of elements present in joResponse object created in previous step using the Count() method of joResponse. Next the JSON key/value pairs – Title, Author, ISBN are read into a JContainer object by traversing JSON using the Property Names as array in JContainer object like “d” and “results” (refer JSON file in Step (1)). The value of the key at after traversing to a Key/value pair of the JSON file is then read using SelectToken passing the name of the key as “Title”, “Author” and “ISBN”.

Next the values of the keys read are stored in the Dictionary object.

Step (5) : Display JSON Data

In the Main() method of the console app, the dictionary object is traversed using a foreach loop and the JSON key value pairs are displayed on the console as follows.

The complete source code of the console app is as follows.

The parsed JSON is read by the C# console app can be seen as follows.

JSON Parsing in C#

This concludes the article – JSON Parsing in C#. I hope you liked this article. If you have any comments, questions or suggestions, please post them using the comments section below this article. I will try to respond at my earliest or somebody else reading the article and your comment will try to respond.

Please subscribe to my blog via email to get regular updates on the latest articles and share this article over social networks like Facebook, Twitter etc. You can use the social sharing buttons and social sharing bar provided to share this on social media.


Share this

Leave a Reply