JSON.NET provides a robust JSON serializer that has both high level and low level components, supports binary JSON, JSON contracts, Xml to JSON conversion, LINQ to JSON and many, many more features than either of the built in serializers. ASP.NET Web API now uses JSON.NET as its default serializer and is now pulled in as a NuGet dependency into Web API projects, which is great.
Dynamic JSON Parsing in JSON.NET
So, why Dynamic JSON parsing rather than strongly typed parsing? Since applications are interacting more and more with third party services it becomes ever more important to have easy access to those services with easy JSON parsing. Sometimes it just makes lot of sense to pull just a small amount of data out of large JSON document received from a service, because the third party service isn't directly related to your application's logic most of the time - and it makes little sense to map the entire service structure in your application.
The easiest way to use JSON.NET is to grab it via NuGet and add it as a reference to your project. You can add it to your project with:
PM> Install-Package Newtonsoft.Json
From the Package Manager Console or by using Manage NuGet Packages in your project References. As mentioned if you're using ASP.NET Web API or MVC 4 JSON.NET will be automatically added to your project.
Alternately you can also go to the CodePlex site and download the latest version including source code:
Let's start with creating some JSON on the fly. It's super easy to create a dynamic object structure with any of the JToken derived JSON.NET objects. The most common JToken derived classes you are likely to use are JObject and JArray.
JToken implements IDynamicMetaProvider and so uses the dynamic keyword extensively to make it intuitive to create object structures and turn them into JSON via dynamic object syntax. Here's an example of creating a music album structure with child songs using JObject for the base object and songs and JArray for the actual collection of songs:
public void JObjectOutputTest()
// strong typed instance
var jsonObject = new JObject();
// you can explicitly add values here using class interface
// or cast to dynamic to dynamically add/read properties dynamic album = jsonObject;
album.AlbumName = "Dirty Deeds Done Dirt Cheap";
album.Artist = "AC/DC";
album.YearReleased = 1976;
album.Songs = new JArray() as dynamic;
dynamic song = new JObject();
song.SongName = "Dirty Deeds Done Dirt Cheap";
song.SongLength = "4:11";
song = new JObject();
song.SongName = "Love at First Feel";
song.SongLength = "3:10";
This will produces a complete JSON structure given below:
"AlbumName": "Dirty Deeds Done Dirt Cheap",
"SongName": "Dirty Deeds Done Dirt Cheap",
"SongName": "Love at First Feel",
Notice that JSON.NET does a nice job formatting the JSON, so it's easy to read and paste into blog posts :-). JSON.NET includes a bunch of configuration options that control how JSON is generated. Typically the defaults are just fine, but you can override with the JsonSettings object for most operations.
The important thing about this code is that there's no explicit type used for holding the values to serialize to JSON. Rather the JSON.NET objects are the containers that receive the data as I build up my JSON structure dynamically, simply by adding properties. This means this code can be entirely driven at runtime without compile time restraints of structure for the JSON output.
Here I use JObject to create a album 'object' and immediately cast it to dynamic. JObject() is kind of similar in behavior to ExpandoObject in that it allows you to add properties by simply assigning to them. Internally, JObject values are stored in pseudo collections of key value pairs that are exposed as properties through the IDynamicMetaObject interface exposed in JSON.NET's JToken base class.
For objects the syntax is very clean - you add simple typed values as properties. For objects and arrays you have to explicitly create new JObject or JArray, cast them to dynamic and then add properties and items to them. Always remember though these values are dynamic - which means no Intellisense and no compiler type checking. It's up to you to ensure that the names and values you create are accessed consistently and without typos in your code.