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:
- json-xslt by Holten Norris / Alan Lewis (used to be hosted at eBay, but the link redirects somewhere else now). XSLT 1.0, not very clean though, all code crammed into one template.
- XSLTJSON by Bram Stein. Seems pretty advanced, supports different JSON syntax conventions, requires XSLT 2.0 however.
- xml2json-xslt by Doeke Zanstra. XSLT 1.0, pretty neat code, uses its own syntax convention. Has several issues: doesn't support XML attributes and arrays of heterogeneous elements.
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:
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 :)
Comments (18)
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
thanks for more detail easier to use JSON as the serialization format
XML to JSON
You are a great programmer.
I will often come to learn a lot from you.
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
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
3gZdb1 <a href="http://dvavqhinrovv.com/">dvavqhinrovv</a>, [url=http://ausnoqvqehfq.com/]ausnoqvqehfq[/url], [link=http://jdjxajvoteiy.com/]jdjxajvoteiy[/link], http://wmdpcmcwltln.com/
oGfHSGHjROvSzSWkDZ
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
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
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
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
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
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
o66rlQ <a href="http://ghmxuoxpguji.com/">ghmxuoxpguji</a>, [url=http://vcewpyotunyx.com/]vcewpyotunyx[/url], [link=http://ftwaebybkxcg.com/]ftwaebybkxcg[/link], http://hsupbzpspuxs.com/

Senior Web Developer
2009-03-13 15:25:07 by Keith Chadwick