Deserializing Various Type Of JSON Data In C#

Nowadays in web applications/APIs JSON Datatype plays a vital role in handling requests and responses.

JSON is Javascript Object notation which is a Data format combination of key values pair in form of Object and easily convertible to other formats.

In this article, will see various methods to Deserialize the JSON Objects.

By using Jsonconvert

Jsonconvert is a static class present in Newtonsoft.Json namespace, which provides various methods to Deserialize the JSON object, will see some of the examples below,

In this example by using DeserializeObject method simple JSON values are converted to Object and provided in Console.

string jsonExample = @ "[ {
    'Name': 'hari',
    'age': '42',
}, {
    'Name': 'Ravi',
    'age': '43'
}]
";
object obj = JsonConvert.DeserializeObject(jsonExample);
Console.WriteLine(obj.ToString());

Output

{
    "Name": "hari",
    "age": "42"
}, {
    "Name": "Ravi",
    "age": "43"
}

Using Jsonconvert class we can convert JSON string mapping with class objects also by using IEnumerable generic collection. Here Book Class is created based on JSON values string, example is below,

string jsonString = @ "[ {
'Bookid': '1',
'BookName': 'Wings of Mind',
'author': 'Arun'
}, {
'Bookid': '12',
'BookName': 'Vetri Nichayam',
'author': 'SugiSivam'
}]
";
var jsonvalues = JsonConvert.DeserializeObject < IEnumerable < Book >> (jsonString).
Select(p => (Id: p.Bookid, item: p)).
ToDictionary(a => a.Id, a => a.item);
foreach(var a in jsonvalues) {
    Console.WriteLine(a.Key.ToString() + " : " + a.Value.BookName + "," + a.Value.author);
}
public class Book {
    public int Bookid {
        get;
        set;
    }
    public string BookName {
        get;
        set;
    }
    public string author {
        get;
        set;
    }
}

Output

1 : Wings of Mind,Arun
12 : Vetri Nichayam,SugiSivam

If JSON values are coming in different combinations of object we can use ExpandoObject class and convert those type of objects,

Example

string jsonFilePath = @ "C:\JsonData\jsonText.json";
string jsonData = File.ReadAllText(jsonFilePath);
var converter = new ExpandoObjectConverter();
dynamic dict = JsonConvert.DeserializeObject < ExpandoObject > (jsonData, converter);
Console.WriteLine(dict.CompanyName + ", " + dict.Owner + ", " + dict.Location);
Console.WriteLine(((ExpandoObject) dict.Product[0]).FirstOrDefault(x => x.Key == "id").Value);
Console.WriteLine(((ExpandoObject) dict.Product[0]).FirstOrDefault(x => x.Key == "name").Value);
Console.WriteLine(((ExpandoObject) dict.Product[0]).FirstOrDefault(x => x.Key == "price").Value);

In this code key values in JSON objects are given in LINQ queries to retrieve the Values of particular keys. Input file and output are below,

InputFile

{
    "CompanyName": "XYZ",
    "Owner": "XXX",
    "CompanyType": "Pvt LTd",
    "Location": "aaaa",
    "Product": [{
        "id": "1",
        "name": "pen",
        "price": "100.00",
    }],
    "Product": [{
        "id": "1",
        "name": "pencil",
        "price": "10.00"
    }]
}

Output

XYZ, XXX, aaaa
1
pencil
10.00


Similar Articles