January 6, 2013

How to convert JSON to XML in ASP.net

Filed under: Tips — Tags: , , — Webopius @ 11:48 pm

Just a short post today. I spent a while on Google trying to figure this out and thought it might help others if I posted a solution.

Many web services now use JSON over XML as their method for data communication. JSON is lightweight compared to XML but is still open and readable.  See the link to the right of this page for more information.


However, once you’ve received a JSON format response from a web service, what if you want to use this within an XmlDocument or XSLT package of some kind? You need to convert it from JSON into XML.

The approach described below does require you to create a data structure (Class) in C# that maps to the JSON data and the principle is:

JSON data -> Deserialize to -> a C# object -> Serialize to -> XML format

We’ll use a WordPress post an an example.

Imagine a WordPress Web Service plugin that has a ‘GetPosts()’ function. This function returns one or more WordPress posts in JSON format, looking like this:

[{"ID":1039,"post_author":"1","post_date":"2013-01-06 14:02:20","post_date_gmt":"2013-01-06 13:02:20","post_content":"This is the content of the post","post_title":"An example Post","post_category":"0","post_excerpt":"","post_status":"publish","comment_status":"open",
"ping_status":"open","post_password":"","post_name":"product-test","to_ping":"","pinged":"","post_modified":"2013-01-07 00:34:31","post_modified_gmt":"2013-01-06 23:34:31","post_content_filtered":"","post_parent":0,"guid":"http:\/\/example.webopius.com\/?p=1039","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","ancestors":[],"filter":"raw"}]

In the example above, one WordPress blog post is returned (with an ID of 1039 and title of ‘An example Post’.

Step 1

In C#, the first step is to create a class to contain the JSON object. So, we create a Post class:

public class Post
  public int	ID;
  public string post_author, post_date, post_date_gmt, post_content, post_title, post_category, post_excerpt,   post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged;
  public string post_modified, post_modified_gmt, post_content_filtered;
  public int post_parent;
  public string guid; 
  public string menu_order, post_type, post_mime_type, comment_count, filter;

For a successful deserialization of JSON into the C# object, it’s important that the field names match the JSON data.

Step 2

Next, we need to deserialize (convert) the JSON data into the C# object. In this example, ‘jsonResponse’ is a string containing the JSON data and we’re going to create a List of ‘Post’ objects that contain all the posts returned by the Web Service:

using System.Web.Script.Serialization;
JavaScriptSerializer ser = new JavaScriptSerializer();
List<Post> postdata = ser.Deserialize<List<Post>>(jsonResponse);

Step 3

OK, now we have a variable ‘postdata’ containing all the posts in a List object. Each item is a Post object. Now, we can serialize this data into an XML object. In this example, we’ll write the XML to a String but you could of course convert into an XmlDocument:

using System.IO;
using System.Xml;
using System.Xml.Serialization;


StringWriter sw = new StringWriter();
XmlWriter xmlw = XmlWriter.Create(sw);
XmlSerializer serializer = new XmlSerializer(typeof(List));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("",""); // Omit namespace
serializer.Serialize(xmlw, postdata, ns);

string theXML = sw.ToString();

The ‘XmlSerializerNamespaces’ lines above aren’t required, but they avoid the XML containing any generated namespace values.

That’s it, your XML string (theXML) will now look something like this:

<?xml version="1.0" encoding="utf-16"?><ArrayOfPost><Post><ID>1039</ID><post_author>1</post_author><post_date>2013-01-06 14:02:20</post_date><post_date_gmt>2013-01-06 13:02:20</post_date_gmt><post_content>This is the content of the post</post_content><post_title>An example Post</post_title><post_category>0</post_category><post_excerpt /><post_status>publish</post_status><comment_status>open</comment_status><ping_status>open</ping_status><post_password /><post_name>product-test</post_name><to_ping /><pinged /><post_modified>2013-01-07 00:34:31</post_modified><post_modified_gmt>2013-01-06 23:34:31</post_modified_gmt><post_content_filtered /><post_parent>0</post_parent><guid>http://example.webopius.com/?p=1039</guid><menu_order>0</menu_order><post_type>post</post_type><post_mime_type /><comment_count>0</comment_count><filter>raw</filter></Post></ArrayOfPost>

  • Tags