Deserializing The Complex JSON Object In C#

Introduction

Serializing and deserializing the JSON object in C# is pretty simple with Newtonsoft.Json NuGet package in C#. In this blog, I’m going to explain how to deserialize complex object. 

Dealing with Complex JSON

Assume we have a complex JSON object like below,

{
    "value": [{
        "id": "/subscriptions/abc247d5-724f-4286-9630-d517572af93f",
        "authorizationSource": "RoleBased",
        "managedByTenants": [],
        "tags": {
            "Department": "Dev",
            "Account": "Sales",
            "Division": "001",
            "CostCenter": "003",
            "test": "jhjh"
        },
        "subscriptionId": "abc247d5-724f-4286-9630-d517572af93f",
        "tenantId": "020a0840-77ca-4d29-9a85-7cf2853d0e98",
        "displayName": "Sample",
        "state": "Enabled",
        "subscriptionPolicies": {
            "locationPlacementId": "PublicAndIndia_2015-09-01",
            "quotaId": "2014-09-01",
            "spendingLimit": "On"
        }
    }, {
        "id": "/subscriptions/abc247d5-724f-4286-9630-d517572af93d",
        "authorizationSource": "RoleBased",
        "managedByTenants": [],
        "tags": {
            "Dept": "Dev"
        },
        "subscriptionId": "abc247d5-724f-4286-9630-d517572af93d",
        "tenantId": "310a0840-77ca-4d29-9a85-7cf2853d0e98",
        "displayName": "Sample Subscription",
        "state": "Enabled",
        "subscriptionPolicies": {
            "locationPlacementId": "PublicAndIndia_2015-09-01",
            "quotaId": "2016-01-01",
            "spendingLimit": "Off"
        }
    }],
    "count": {
        "type": "Total",
        "value": 2
    }
}

This can be easily deserialized using Newtonsoft.json

First breakdown all the entities by creating a model class

RootObject.cs

public class RootObject {
    public List < Value > value {
        get;
        set;
    }
    public Count count {
        get;
        set;
    }
}

Value.cs

public class Value {
    public string id {
        get;
        set;
    }
    public string authorizationSource {
        get;
        set;
    }
    public string[] managedByTenants {
        get;
        set;
    }
    public Dictionary < string, string > tags {
        get;
        set;
    }
    public string subscriptionId {
        get;
        set;
    }
    public string tenantId {
        get;
        set;
    }
    public string displayName {
        get;
        set;
    }
    public string state {
        get;
        set;
    }
    public SubscriptionPolicies SubscriptionPolicies {
        get;
        set;
    }
}

SubscriptionPolicies.cs

public class SubscriptionPolicies {
    public string locationPlacementId {
        get;
        set;
    }
    public string quotaId {
        get;
        set;
    }
    public string spendingLimit {
        get;
        set;
    }
}

Count.cs

public class Count {
    public string type {
        get;
        set;
    }
    public int value {
        get;
        set;
    }
}

Code to deserialize the Complex JSON string,

After deserialization,

Happy Coding!!!