Simple Json C# Correct Way to Deserialize Dictionary

We are using the RESTSharp Library to work with third-party REST-based APIs, which by default support XML and JSON deserialization. However, their documentation for Deserialization covers a limited, simple example, which was not sufficient to help us consume Complex JSON output.

We are using http://json2csharp.com/ to convert JSON string to C# class. Which works perfectly when a JSON string is in a simple structure. but when it comes to Deserialize to JSON to Dictionary< string, class> the class generated using json2csharp.com fails.

Example:

Consider following complex JSON response which requires Dictionary<string, class>.

JSON:


"response": {
	"1007": {
		"total": {
			"inStock": 10,
			"onHand": 9,
			"allocated": 1
		}
	},
	"1008": {
		"total": {
			"inStock": 0,
			"onHand": 0,
			"allocated": 0
		}
	}
}

Output after converting JSON to C# class using converter tool(https://json2csharp.com/):


public class Total
{
    public int InStock { get; set; }
    public int OnHand { get; set; }
    public int Allocated { get; set; }
}

public class StockDetails1007
{
    public Total Total { get; set; }
}

public class StockDetails1008
{
    public Total Total { get; set; }
}

public class Response
{
    public StockDetails1007 Stock1007 { get; set; }
    public StockDetails1008 Stock1008 { get; set; }
}

public class RootObject
{
    public Response Response { get; set; }
}

Wrong way to put Dictionary:


public class Response
{
    public Dictionary<string, Product> Products { get; set; }
}

public class RootObject
{
    public Response Response { get; set; }
}

The right way to put Dictionary:


public class RootObject
{
    // Corrected the Dictionary syntax by using proper < and > characters
    public Dictionary<string, Product> Response { get; set; }
}

public class Product
{
    public Total Total { get; set; }
}

public class Total
{
    public int InStock { get; set; }
    public int OnHand { get; set; }
    public int Allocated { get; set; }
}

I hope this article saves time for you.

Thank you…!!

Article by

Chintan Prajapati

Chintan Prajapati is the Founder and CEO of Satva Solutions and a seasoned computer engineer with over two decades of experience in the software industry. His expertise spans Accounting & ERP Integrations, Robotic Process Automation, and the development of technology solutions built around leading ERP and accounting platforms with a particular focus on responsible AI and machine learning in fintech.Chintan holds a BE in Computer Engineering and carries an impressive roster of certifications, including Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, Certified QuickBooks ProAdvisor, and Xero Developer.Over the course of his career, he has made a measurable impact on the accounting industry consulting on and delivering integration and automation solutions that have collectively saved thousands of man-hours. His writing aims to offer readers practical, insight-driven advice on harnessing technology to unlock greater business efficiency.When he steps away from the desk, Chintan can be found trekking through mountain trails or watching birds in the wild. Grounded in the philosophy of delivering the highest value to clients, he continues to champion innovation and excellence in digital transformation from his home base in Ahmedabad, India.