Manipulating JSON using Json.NET


Json.NET makes it convenient to manipulate JSON in C# using dynamic programming. Let’s start with a JSON representation we want to create

{
	"menu": {
		"id": "file",
		"value": "File",
		"popup": {
			"menuitem": [
				{
					"value": "New",
					"onclick": "CreateNewDoc()"
				},
				{
					"value": "Open",
					"onclick": "OpenDoc()"
				},
				{
					"value": "Close",
					"onclick": "CloseDoc()"
				}
			]
		}
	}
}

Here’s how the Json.NET object representation can be created in C# using dynamic programming

dynamic jobj = JObject.FromObject(new 
{
	menu = new
	{
		id = "file",
		value = "File",
		popup = new
		{
			menuitem = new []
			{
				new
				{
					value = "New",
					onclick = "CreateNewDoc()"
				},
				new
				{
					value = "Open",
					onclick = "OpenDoc()"
				},
				new
				{
					value = "Close",
					onclick = "CloseDoc()"
				}
			}
		}
	}
});

To serialize it

var json = JsonConvert.SerializeObject(jobj);
Console.WriteLine(json);

To deserialize JSON string representation to dynamic object

dynamic jobj = JsonConvert.DeserializeObject(json);
Console.WriteLine(jobj.menu.id);

It is fairly easy to extend the object representation and add new items

jobj.foo = new JArray()
{
	new JObject() {
		new JProperty("bar", 10)
	},
	new JObject() {
		new JProperty("bar", 20)
	}
};

That adds a new property to jobj called foo that references a new array containing two objects.

Create XML documents in LINQ


I’ve created XML documents using string concatenation, and DOM implementations, in the past. LINQ in .NET provides an interesting mechanism for document creation.

I’ll use the following XML document obtained from IETF’s PIDF-LO spec, as an example

<presence xmlns="urn:ietf:params:xml:ns:pidf" xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:gp="urn:ietf:params:xml:ns:pidf:geopriv10" xmlns:gml="http://www.opengis.net/gml" xmlns:cl="urn:ietf:params:xml:ns:pidf:geopriv10:civicAddr" entity="pres:mike@seattle.example.com">
	<dm:device id="mikepc">
		<gp:geopriv>
			<gp:location-info>
				<gml:Point srsName="urn:ogc:def:crs:EPSG::4326">
					<gml:pos>-43.5723 153.21760</gml:pos>
				</gml:Point>
				<cl:civicAddress>
					<cl:FLR>2</cl:FLR>
				</cl:civicAddress>
			</gp:location-info>
			<gp:usage-rules/>
			<gp:method>Wiremap</gp:method>
		</gp:geopriv>
		<dm:deviceID>mac:8asd7d7d70cf</dm:deviceID>
		<dm:timestamp>2007-06-22T20:57:29Z</dm:timestamp>
	</dm:device>
</presence>

Note that the document uses multiple schemas. This is how LINQ’s functional document creation can be used to recreate the above document using C#

XNamespace pidf = "urn:ietf:params:xml:ns:pidf";
XNamespace dm = "urn:ietf:params:xml:ns:pidf:data-model";
XNamespace gp = "urn:ietf:params:xml:ns:pidf:geopriv10";
XNamespace gml = "http://www.opengis.net/gml";
XNamespace cl = "urn:ietf:params:xml:ns:pidf:geopriv10:civicAddr";
	
XElement presence = new XElement(
	pidf + "presence",
	new XAttribute("xmlns", pidf.NamespaceName),
	new XAttribute(XNamespace.Xmlns + "dm", dm.NamespaceName),
	new XAttribute(XNamespace.Xmlns + "gp", gp.NamespaceName),
	new XAttribute(XNamespace.Xmlns + "gml", gml.NamespaceName),
	new XAttribute(XNamespace.Xmlns + "cl", cl.NamespaceName),
	new XAttribute("entity", "pres:mike@seattle.example.com"),
	new XElement(
		dm + "device",
		new XAttribute("id", "mikepc"),
		new XElement(
			gp + "geopriv",
			new XElement(
				gp + "location-info",
				new XElement(
					gml + "Point",
					new XAttribute("srsName", "urn:ogc:def:crs:EPSG::4326"),
					new XElement(gml + "pos", "-43.5723 153.21760")
				),
				new XElement(
					cl + "civicAddress",
					new XElement(cl + "FLR", 2)
				)
			),
			new XElement(gp + "usage-rules", ""),
			new XElement(gp + "method", "Wiremap")
		),
		new XElement(dm + "deviceID", "mac:8asd7d7d70cf"),
		new XElement(dm + "timestamp",  DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ"))
	)
);

XDocument presenceDocument = new XDocument(presence);
Console.WriteLine(presenceDocument.ToString());