Home > Articles > Programming > C#

.NET Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Simple JSON Processing

Last updated Mar 14, 2003.

JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for computers to parse and generate, and easy for humans to read and write. Although based on a subset of the JavaScript standard, it is a language independent text format that uses conventions that will be familiar to most programmers. JSON has been described by some as a "poor man's XML," due to its simplicity and brevity.

JSON is built on two structures: a collection of name/value pairs, and an ordered list of values. These two structures roughly correspond to the language concepts of an object and an array (or collection), respectively.

As I indicated, the JSON format is simpler than XML. It lacks many features that make XML the rich format that it is. However, many applications that use XML as a data interchange format use only a very small subset of the full XML capabilities: simple elements and attributes--concepts that are easily duplicated more succinctly by JSON.

JSON in .NET

The .NET runtime library includes complete support for JSON through the JavaScriptSerializer class which is available in the System.Web.Script.Serialization namespace. AJAX applications written in .NET use JavaScriptSerializer to serialize objects that will be sent to clients, and for deserializing data that clients sent to the application. This works well and is the preferred method of JSON processing in .NET AJAX applications.

If you're not writing an AJAX application, you can serialize and deserialize JSON data using the DataContractJsonSerializer class.

However, serialization and deserialization assume a number of things: chiefly among them that you have defined objects that correspond to the JSON data. It's not possible with JavaScriptSerializer to deserialize JSON into an object of an unknown type. The type resolver associated with the serializer must be able to resolve the object type. This restriction isn't limited to JSON serializers. All serialization assumes that you're working with defined types.

Applications that want to read ad hoc JSON data need another way to do it, just as applications that want to read arbitrary XML do. The answer for XML is XmlReader and XmlWriter. For JSON it's the JsonReaderWriterFactory class, which provides a very simple interface for reading and writing arbitrary JSON.

Reading JSON data

To read JSON data, you call JsonReaderWriterFactory.CreateJsonReader, passing it the data that you want to read. The data can be an array of bytes or a stream. The return value is an XmlReader object instance that you can then use like any other XmlReader. Under the hood, the serializer converts the JSON that it reads into simple XML elements and properties and presenting them in the well known and easy to use XmlReader interface.

I find this a particularly elegant solution because I don't have to learn new JSON terminology or a new API just to read a few files in this new (to my application) interchange format. I recently had to make an application understand JSON, and at first I was resistant because my entire application was based on using XML as the data interchange format. But with JsonReaderWriterFactory, it took just a few minutes to put together a solution that creates an XmlReader interface to the JSON data so that the rest of my application can read it.

Writing JSON data

As you probably expect, JsonReaderWriterFactory.CreateJsonWriter creates an XmlWriter instance to which you write XML. Internally, that XML is converted to JSON and outputs it to the stream that you passed in the constructor. Again, this makes it almost trivial to extend an existing XML-centric application so that it will output data in JSON format.

Parting thoughts

As I mentioned, JSON is not as rich as XML so there are things that you can express in XML that have no JSON equivalent. As a result, you have to be aware of the differences and limitations as discussed in the MSDN article Stand-Alone JSON Serialization. I think you will find, though, that this simple JSON-to-XML (and back) conversion will easily accommodate most of your data interchange needs without any trouble.

If you get errors when reading JSON with the XmlReader instance returned by CreateJsonReader, don't do like I did and assume that the problem is with the JSON-to-XML conversion. Instead, copy the JSON in question and run it through JSONLint. You'll most likely find that the JSON is in error.

Although it's not as rich or as expressive as XML, I've found JSON to be an adequate replacement for XML in my applications. For simple data interchange that uses only a very small subset of XML, JSON can be a direct replacement. The advantages are that it's typically less data because it's less verbose than XML, and it's easy to share the data with JavaScript applications and others that don't "speak" XML. The existence of JsonReaderWriterFactory means that it's very easy to make your application speak both languages.