Tuesday, July 18th, 2006

Json.NET 1.1: Converting between XML and JSON

Category: JavaScript, Library

<p>James Newton-King has updated his Json.NET library to version 1.1.

The major new feature is that you can now almost seamlessly convert between XML and JSON:

javascript
< view plain text >
  1. XmlDocument doc = new XmlDocument();
  2. doc.LoadXml(@"< ?xml version=""1.0"" standalone=""no""?>
  3. <root>
  4.   <person id=""1"">
  5.     <name>Alan</name>
  6.     <url>http://www.google.com</url>
  7.   </person>
  8.   <person id=""2"">
  9.     <name>Louis</name>
  10.     <url>http://www.yahoo.com</url>
  11.   </person>
  12. </root>");
  13.  
  14. string jsonText = JavaScriptConvert.SerializeXmlNode(doc);
  15. //{
  16. //  "?xml": {
  17. //    "@version": "1.0",
  18. //    "@standalone": "no"
  19. //  },
  20. //  "root": {
  21. //    "person": [
  22. //      {
  23. //        "@id": "1",
  24. //        "name": "Alan",
  25. //        "url": "http://www.google.com"
  26. //      },
  27. //      {
  28. //        "@id": "2",
  29. //        "name": "Louis",
  30. //        "url": "http://www.yahoo.com"
  31. //      }
  32. //    ]
  33. //  }
  34. //}
  35.  
  36. XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(jsonText);
  37.  
  38. Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);

Posted by Dion Almaer at 7:21 am
8 Comments

++++-
4.1 rating from 29 votes

8 Comments »

Comments feed TrackBack URI

There is another XML JSON converter:

http://weblogs.asp.net/mschwarz/archive/2006/06/19/XML-_2D003E00_-JSON-Serialization.aspx

Comment by Michael — July 18, 2006

There is already another one here:

http://weblogs.asp.net/mschwarz/archive/2006/06/19/XML-_2D003E00_-JSON-Serialization.aspx

Comment by Michael Schwarz — July 18, 2006

These are the rules I came up with when converting XML to JSON. You can see some in the example above.
-
* Elements remain unchanged.
* Attributes are prefixed with an @.
* Single child text nodes are a value directly against an element, otherwise they are accessed via #text.
* The XML declaration and processing instructions are prefixed with ?.
* Character data, comments, whitespace and significate whitespace nodes are accessed via #cdata-section, #comment, #whitespace and #significate-whitespace respectively.
* Multiple nodes with the same name at the same level are grouped together into an array.
* Empty elements are null.
-
Unfortunately you lose some ordering information when multiple nodes of the same name are grouped together but apart from that the transition back and forth (including namespace data) is pretty good.

Comment by James Newton-King — July 18, 2006

Interesting Finds: July 18, 2006

Trackback by Jason Haley — July 18, 2006

James’ conversion rules are pretty good, but there’s one edge case you need to stay aware of relating to the multiple-node-array thing. If you have a document which normally returns multiple nodes (say a set of tags) you might write code against the JSON conversion that expects an array – but this will break if only a single category is present in the XML as the convertor will not know to turn it in to an array. This can be quite a nasty gotcha if you aren’t expecting it.

Comment by Simon Willison — July 19, 2006

Yes that could be a problem. The same issue also applies to single text nodes suddenly having additional content and no longer being a name/value against an element. I was thinking about having an overload to take a schema, which could then place nodes with the potential to have more than one in an array.

Comment by James Newton-King — July 19, 2006

Link Listing – July 20, 2006

Announcing Teamprise 1.1 and Vault 3.5 [Via: ]
Creating Smaller Virtual Machines [Via: ]
Customizing…

Trackback by Christopher Steen — July 20, 2006

now if only SQL Server (et al.) had a built-in “ForJSON” that worked the same (only better) than “ForXML”

Comment by lb — July 23, 2006

Leave a comment

You must be logged in to post a comment.