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
4
5
6
7
8
9
into
1{
2 one: 'foo',
3 two: 'bar',
4 three: 'foobar'
5}
or
1
2
3
4
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
4
5
6
7
8
9
10
11
12
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() {...
Comments (9)
Marcus Tucker, at 25/11/'10 13:48, said:
Mitya, at 25/11/'10 15:38, said:
James, at 2/12/'10 11:01, said:
Mitya, at 2/12/'10 11:06, said:
Jay Koutavas, at 2/12/'10 13:06, said:
Mitya, at 2/12/'10 13:08, said:
Mitya, at 2/12/'10 13:09, said:
Ciul, at 3/01/'11 13:07, said:
Mitya, at 3/01/'11 19:26, said: