Home & blog  /  Scripts  /  view post  /

XML-to-JSON convertor and remapper

Overview

This is a jQuery plugin for converting data from XML to JSON format. However its real power lies in its ability to also remap data to a different structure. This can be useful when you have a plugin or application that requires JSON data, but the data is coming from an XML feed that you don't have structural control over. Data is remapped to the structure you need.

Example (concept)

So you could turn

1

2    

3         foo

4         bar

5        

6             foobar

7        

8    

9

into

1{

2     one: 'foo',

3     two: 'bar',

4     three: 'foobar'

5}

or

1

2    

3         foo

4         bar

5    

6

into

1{

2     one: 'foo',

3     two: {

4         two_b: 'bar'

5     }

6}

Usage & params

The plugin runs on your XML data. It accepts two arguments:

$(xmldata).XMLToJSON(storer, iterationNodePath, remap);

storer (array) - an array that will hold the returned JSON data.

iterationNodePath (string) - a space-separated path to the iteration node, i.e. the repeated node that will form the sub-objects of your JSON data, e.g. root item or, if RSS, perhaps rss channel item

remap (object) - a map of XML paths (left) to JSON paths (right), the latter allowing you to stipulate the structure you require.

The plugin would typically run inside a success callback function after loading the XML via AJAX.

Example (with JAWS)

Take the example of my JAWS news ticker. That is populated with JSON data, in the following format:

1[

2     {

3         "title": "title 1",

4         "url": "news.php?story=1",

5         "moreInfo": {

6             "info": "description 1",

7             "image": "images/someImage.jpg"

8         }

9     },

10     {

11         "title": "title 2",

12         "url": "news.php?story=2",

13         "moreInfo": {

14             "info": "description 2",

15             "image": "images/someImage2.jpg"

16         }

17     }

18     //etc

19]

But imagine the feed you've been asked to use to populate it is in XML, and in the following format:

1

2    

3         title 1

4         news.php?story=1

5         description 1

6         images/someImage1.jpgthumb>

7    

8    

9         title 2

10         news.php?story=2

11         description 2

12         images/someImage2.jpgthumb>

13    

14

Since the above JSON and XML structures differ notably (both in node names and in levels, e.g. the JSON has a 'moreInfo' node with its own children), you'd need to not only convert but also remap.

Here's how:

$(xmldata).XMLToJSON(jawsData = [], remap = {headline: 'title', description: 'moreInfo.info', thumb: 'moreInfo.image'});

In the remap object, the the dots separate. So for example we not only rename the description to info, we move it inside a moreInfo object, thus changing the hierarchical nature of the data.

The source download includes this scenario, so you can see the conversion working.

Notes

Loading jQuery

By default, this script assumes you're loading jQuery via Google (why might you do this?). If, instead, you're loading jQuery from a local copy in your site folder, update the document ready handler (DRH) in the script file:

1//loading jQuery via Google

2google.setonloadcallback(function() { ...

3 

4//loading jQuery from local copy (use either)

5$(document).ready(function() {...

6$(function() {...

post new comment

Comments (9)

Marcus Tucker, at 25/11/'10 13:48, said:
It's fantastic that you've done this, I've been looking for something to do this kind of remapping for ages and haven't had the time to write one myself!

What I've been intending to use it for is having a single XML data format for my image galleries, then letting the user switch between many different gallery / slider / zoomer / etc views (i.e. various different jQuery plugins for image browsing/viewing) and transforming the XML data on the fly into whatever JSON structure each specific plugin requires!

Thanks!
Mitya, at 25/11/'10 15:38, said:
Thanks a lot, Marcus. It wasn't easy; well, the conversion part was, but the remapping was a real headache (see code) but, as you say, it's necessary in certain situations. I built it out of need, actually; I was trying to plugin my JAWS ticker for a client, but the feed was XML. Hence.
James, at 2/12/'10 11:01, said:
Might need some help with this.. If I take your feed for example:  and put this url into url: 'xml.xml' on line 11 in index.htm from the download package you provided the latest news doesn't appear? I also tried copying and pasting the source of your feed into xml.xml and one entry appears but when clicking on it the entry is blank. In order for this to work I have to change the settings under remap to match your feed? So instead of Sample headline 3 it should be XML-to-JSON convertor and remapper ?
Mitya, at 2/12/'10 11:06, said:
James - e-mail me your starting code and the format of the JSON your require and I'll look into this. This script went through extensive testing so I'm confident we can crack it.
Jay Koutavas, at 2/12/'10 13:06, said:
Does your library handle XML node attributes?
Mitya, at 2/12/'10 13:08, said:
James - this is now fixed. The issue was you are reading in an RSS feed, not a normal XML feed, the difference being the former traditionally has 3 levels between and including the root and the iteration node (e.g. rss > channel > item) whereas a simple XML feed would probably be just two levels (e.g. root > item).

I am posting the revised source code to the site now and will post to my blog and the script's changelog re: this issue and its fix.

Thanks
Andy
Mitya, at 2/12/'10 13:09, said:
Jay Koutavas - presently, no, but that's the obvious next step. Expect implementation of that by next week. If you get my blog RSS feed you'll get an alert when that's posted.
Ciul, at 3/01/'11 13:07, said:
Hi there, greate code :)

Could you show an example of how does it handle XML nodes attributes?, I mean, how ther are converted into the JSON.

Thanks
Mitya, at 3/01/'11 19:26, said:
Thanks, Ciul. Actually the issue of XML attributes is something I'm working on currently. It's a tough one; XML attributes do not translate well to JSON, as you cannot specify attributes of objects in JS, only child nodes. I am working on a strategy, though. Stay posted!
post new comment