Despite building applications with C# for years, I was frustrated today with some basic JSON parsing. This is something that I have done hundreds of times in Ruby and Python, where parsing JSON consists of deserializing into a Hash (Ruby) or Dictionary (Python) on the fly without defining a class, or mapping, beforehand. It’s easy to build the objects on the fly in the dynamic languages. But, with C# being a statically typed language, it’s taken a long time to get to that point. This becomes important as you consume many JSON endpoints from a service, it saves us time if we don’t have to model all of these responses in C# before making requests. This proved to be an opportunity to catch up with the state of art in C# for dynamically parsing JSON.


Consume a JSON string (such as the following) in a C# program, hopefully creating some sort of dynamic object on the fly that we can work with like any other C# object, and then doing something with the object: saving it in database, sending it elsewhere or rendering to a screen.

<br /> {"results":[<br /> {"employeename":"name1","employeesupervisor":"supervisor1"},<br /> {"employeename":"name2","employeesupervisor":"supervisor1"},<br /> {"employeename":"name3","employeesupervisor":["supervisor1","supervisor2"]}<br /> ]}<br />

Step 1 – Parse JSON, dynamically converting it into a C# object

Again, trying to get up to speed and taking the most “modern” approach by dynamically parsing without pre-defining classes or mappings. Looking at some samples, I see mention of a JsonArray type, and a JsonObject in conjunction with the newer .NET Http Client library. But, I cannot seem to use these, and am not sure if they still exist. So, I’m going to quickly switch gears and use Json.NET, since it’s essentially been the de-facto JSON parsing library for .NET applications for years, and that doesn’t seem to be any different now.

I’ve said “dynamic” many times already, and as luck would have it, .NET 4 introduced dynamic types. dynamic types bypass compile time checking. Great, that seems promising. Rick Strahl has a post where he introduces us to Json.NET’s dynamic support, using JObject and JArray

Step 2 – Do something with the C# object

So, great, we have a nice dynamic c# object, but did you notice something in that sample? The last record’s supervisor has disparate value types. This is perfectly valid JSON, per the JSON spec:

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Nowhere does it say that the value type has to be same for the property across all instances in an array, which if they were all the same type, we’d be done already. So, how do handle this case with our Json.NET JObject instance. As a demonstration, let’s say the requirement is to just take the first one if it’s an array.


Here’s a little program to demonstrate

Overall, the Json.NET looks like the solution we were looking for, with a concise few lines of code to parse and process the result. It took entirely too long to get to this point though, both in terms of the language and in my work to understand it today. Hopefully we’ll see more acceptance of dynamic parsing in C# and start moving more examples and codebases in that direction.