Blog

XML to JSON

2009-01-21 23:14:07 by Martynas Jusevičius

Lately I got involved with some AJAX, namely dynamic maps and autocompletion. It is much more easier to use JSON as the serialization format than XML because no complex parsing is needed, JSON structures automagically become JavaScript objects. So I knew I need my webservice endpoints to return JSON, but the DIY Framework is based on XML serialization of objects.
Luckily, that was no problem at all, since JSON and XML are basically different syntaxes for the same data model, and I knew XML to JSON conversion could be done using an XSLT stylesheet. And of course I found several existing ones:

I wanted support for XSLT 1.0 and XML attributes and arrays, but none of them really did the job. So I decided to fix the issues of xml2json-xslt by adding attribute and array support, and here is the result:

xml2json.xsl

I'm not really sure if it follows any syntax convention, but it does the job for me. Attributes are serialized in the same fashion as elements, this can be switched off using include-attrs parameter (on by default). Children elements with the same name are grouped using Muenchian method and put into arrays.

I've only tested it on relatively simple and flat XML, so bug reports are welcome :)

Digg Digg this! del.icio.us del.icio.us!

Comments (31)

Senior Web Developer

2009-03-13 15:25:07 by Keith Chadwick

I worked on a similar implementation back in December as know one had done a pure XSL implementation at the time. Finally getting around to blogging it and I find that you posted one up in late January. My version will provide for different styles of JSON, such as BadgerFish, and perhaps when I have completed the post you can have a look. You are doing some things I had not thought of and vice versa.

Cheers
Keith Chadwick

Article JSON

2009-05-23 06:46:01 by lissa

thanks for more detail easier to use JSON as the serialization format

XML to JSON

2009-05-23 10:33:49 by Bisnis Online

You are a great programmer.
I will often come to learn a lot from you.

livsfsmdmfv

2009-05-31 08:40:21 by livsfsmdmfv

QFK58l <a href="http://ealjtyojcvxx.com/">ealjtyojcvxx</a>, [url=http://pscnisdxcukw.com/]pscnisdxcukw[/url], [link=http://dskrackbgimd.com/]dskrackbgimd[/link], http://wtdmewoywmrm.com/

Thanks

2009-06-12 19:41:53 by Furniture

great info! i'd love to read more articles. thanks

2009-06-23 19:41:02 by Jörn Heid

Great. I just put some <xsl:text> around those {,},[,] and , so that the resulting JSON is a little bit smaller.

2009-08-17 17:22:36 by kamal

hi,

I tried your code but it doesn't work with my XML file wich is the bellow:

<feed xmlns="http://www.w3.org/2005/Atom">

<subtitle>
Un titre secondaire.
</subtitle>
<link href="">
<updated>
2009-12-13T18:30:02Z
</updated>
<author>
<name>

Digiposte
</name>
<email>
xx@xx.fr
</email>
</author>
<id>
urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6
</id>
<entry>
<category>Facture</category>
<id>documentsecurise/1/3234</id>
<author>
<name>EDF</name>
<uri></uri>
</author>
<link rel="self" href="documentsecurise/1/3234" type="application/pdf">
<published>2009-08-10T09:19:25.188Z</published>
<updated>2009-08-10T09:19:25.188Z</updated>
<summary></summary>
<content type="xhtml">
<div class="infosEmetteur">
<span class="valid">2010-05-18T24:00:00.205Z</span>
<span class="due">2009-06-14T10:07:20.205Z</span>
<span class="idFlux">EDF456</span>
<span class="relation">FAC2783</span>
<span class="referenceEmetteur">FAC4356</span>
<span class="importance">2</span>
<ul class="themes">
<li>energie</li>
<li>facture</li>
<li>electricite</li>
</ul>
<span class="montant">12.55</span>
<span class="devise">EUR</span>
<span class="paye">2</span>
</div>
<div class="gestionDigiposte">
<span class="urlVignette">http://www.xxx.com/documents/FORF2_vignette.png</span>
<span class="tagSystem">1.classeur.factures.appartement.edf</span>
<span class="statut">read</span>
<span class="alerte">V</span>
</div><div class="infosAbonne">
<ul class="tagsAbonne">
<li>facture</li>
<li>maison</li>
<li>edf</li>
</ul>
</div>
<div class="checksum">86f7e437faa5a7fce15d1ddcb9eaeaea377667b8</div>
</content>
</entry>
</feed>

2009-08-17 17:24:13 by kamal

it would be great if you povide me some help regards kamal

yfJrkUEXMTjIEOLioMU

2009-08-31 12:24:19 by nwhjlf

3gZdb1 <a href="http://dvavqhinrovv.com/">dvavqhinrovv</a>, [url=http://ausnoqvqehfq.com/]ausnoqvqehfq[/url], [link=http://jdjxajvoteiy.com/]jdjxajvoteiy[/link], http://wmdpcmcwltln.com/

oGfHSGHjROvSzSWkDZ

2009-09-01 06:59:57 by Jack

Acai Berry On Oprah usa Acai Berry Weoght Loss Formula usa Amazon Thunder Acai Berry Juice usa Extreme Acai Berry Testimonials usa Acai Berry Testimonials For Asthmatics

QfpUuDliwvJwOnxPNFd

2009-09-01 09:39:15 by Lee

Consumer Reports Acai Berry Diet usa Acai Berry Diet Reviews usa Acai Berry And Inflamation usa Take Singulair With Acai Berry usa Stores That Sell Acai Berry Weightloss

ZegEOxQXRzJNa

2009-09-01 22:49:33 by Lee

Acai Berry Ratings usa Where To Get Acai Berry usa Acai Berry Extract Scam usa Acai Berry Side Effects Whole Foods usa Acai Berry Delaware

qcTbwTWClaoUG

2009-09-02 11:53:38 by Music

Does Acai Berry Supreme Really Work usa Acai Berry Uses usa Picture Of The Acai Berry usa Purchase Acai Berries usa Acai Berry Real Or Not

WPwngBnAknKP

2009-09-03 14:33:56 by Riston

Vicodin Mg usa Vicodin Nausea usa Vicodin No Prescription usa Vicodin No Rx usa Vicodin On Line

Doesn't handle this

2009-09-13 19:32:14 by Al Pacifico

With the following xml doc:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tree fruit="pear">partridge</tree>

the output is:

{"tree":{"fruit":"pear","partridge"}

which is not valid JSON.

How should this be handled?

Thanks.

PGSnHqPptmzbiNZ

2009-11-20 10:04:17 by wujypeid

crmrP8 <a href="http://iripignjebqc.com/">iripignjebqc</a>, [url=http://zenpwiqftufg.com/]zenpwiqftufg[/url], [link=http://hhczaxsxxrug.com/]hhczaxsxxrug[/link], http://yfzgntfbsmgs.com/

2009-11-25 17:20:01 by Alan

I just wanted to second Al Pacifico's concerns, I am also having this problem:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tree fruit="pear">partridge</tree>

the output is:

{"tree":{"fruit":"pear","partridge"}

which is not valid JSON.

DLQlnlKplKu

2009-12-02 10:33:33 by vyrerye

o66rlQ <a href="http://ghmxuoxpguji.com/">ghmxuoxpguji</a>, [url=http://vcewpyotunyx.com/]vcewpyotunyx[/url], [link=http://ftwaebybkxcg.com/]ftwaebybkxcg[/link], http://hsupbzpspuxs.com/

sjeefkmaiy

2010-03-22 15:23:21 by sjeefkmaiy

KEAuyn <a href="http://kshurhfbwvyg.com/">kshurhfbwvyg</a>, [url=http://gparplionpki.com/]gparplionpki[/url], [link=http://mumtcsfgtyxk.com/]mumtcsfgtyxk[/link], http://vbtepuwhnjdv.com/

2010-05-20 23:48:56 by Claudius Teodorescu

Hi,

Your xml2json is very nice and useful. I had only to add "{" before the result, in order to evaluate it to an object in JS.

Thank you

Margb

2010-06-17 00:16:56 by دردشة مصرية

very nice and usef2ul. I had only

http://www.passforsure.co.uk/000-201.htm

2010-08-06 09:03:49 by pass4sure 000-201

Nice

http://www.passforsure.co.uk/000-331.htm

2010-08-06 09:04:08 by pass4sure 000-331

Amazing

http://www.passforsure.co.uk/000-973.htm

2010-08-06 09:04:25 by pass4sure 000-973

I like it

http://www.passforsure.co.uk/jn0-141.htm

2010-08-06 09:04:45 by pass4sure JN0-141

Amazing

nice

2010-08-11 09:52:24 by blog seo

great info! i'd love to read more articles. thanks

More Improvements

2010-08-17 04:33:43 by Brendan

To anybody who stumbles across this. I've modified the file further and fixed the problems mentioned above with

<tree fruit="pear">partridge</tree>

producing invalid JSON.

You can find the project here on GitHub: http://github.com/bdoms/xml2json

Patches and bug reports are welcome!

ugg boots

2010-08-23 05:28:44 by ugg boots

i like<a href="http://www.buyuggsite.com ">ugg boots</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">uggs</a>,<a href="http://www.buyuggsite.com ">ugg australia</a>,<a href="http://www.buyuggsite.com ">ugg classic</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">ugg boots uk</a>,especlially like<a href="http://www.buyuggsite.com ">ugg classic tall</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">tall ugg boots</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">short ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">uggs sale</a><a href="http://www.buyuggsite.com ">buy ugg</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">buy ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">discount ugg</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">buy uggs</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">ugg boots online</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">discount uggs</a><a href="http://www.buyuggsite.com/ugg-classic-short-5825-boots-c-9.html ">buy cheap ugg boots</a>

Comment

2010-08-23 14:29:35 by braindumps

Wow. Thank you for a really insightful post.

louboutin shoes

2010-08-31 12:04:28 by louboutin shoes

<a href="http://www.whatforher.com">top quality christian</a>

Car DVD NAV - GPS Systems For Cars

2010-09-02 06:37:07 by Autocarplaza

best car dvd nav online store

New comment






No HTML allowed.