XML to JSON Conversion Using XSLT - for eBay or other web services
- I just uploaded my XSLT 1.0 stylesheet that converts any XML data to
JSON. Check out the XML 2 JSON project homepage
<https://json-xslt.codebase.ebay.com/> on the eBay Codebase (there is
a download link to the most recent version on that page). This is open
sourced under the CDDL, and is based off of some code that was written
by Holten Norris and placed under the CDDL at the Ajaxian conference a
few months back.
Why is this important? I have one goal -- make it easy to use eBay Web
know, you can use JSON-formatted data, wrapped in a callback, to get
around the same-domain restriction that keeps you from calling
output option for their web services, and this gives you the same thing
for eBay's REST-enabled web services (or for any others out there they
may have an XSLT server option -- you know who you are, Jeff)
I presented this back at the eBay Developers Conference a few weeks
back, which, amazingly, appears to be the same day that this other XML
to JSON conversion project <http://www.bramstein.nl/xsltjson/>
launched. The code is totally different -- I didn't know about it until
about an hour ago when I was reading the newsgroup messages. Bram's code
uses XSLT 2.0, whereas mine uses XSLT 1.0. I'm not surprised that I
wasn't the only one working on this, because it opens up lots of
interesting possibilities for client-side code that uses third-party web
Jason asked in this post
<http://groups.yahoo.com/group/json/message/427?l=1> how this is
different from other approaches? Bram's and mine are the first XSLT
solutions I'm aware of that will convert any XML data into JSON,
regardless of schema. Other solutions I've seen, including one from
PostApp <http://www.postapp.com/ebay/> that inspired me to do this,
code their XSLT to a specific schema, requiring XSLT changes to support
additional calls. With this XSLT, when eBay brings out additional
REST-enabled calls (such as the GetContextualKeywords call that powers
the AdContext project -- sign up for the beta
<http://affiliates.ebay.com/ads/adcontext/> today!) there will be no
additional work needed (cross your fingers) to support those calls. This
means that it should work with other types of web services too. The only
dependency on eBay that I know about is the xsl:stylesheet line that
strips out the eBay namespace from the XML.
The format of the data returned mimics that returned by Yahoo's JSON
output for their web services. Elements with attributes and textual data
are converted to objects, with the attributes becoming child nodes, and
the text content going in a child node named content. When there are
multiple child elements with the same name, all the child elements are
wrapped in an array.
The code is not 100% baked, but it works on most eBay REST API searches.
The one issue I know about is that quotes in text content are not
converted properly. There may be other text that I need to escape, and
I'll be updating it soon with that support.
If you want to contribute to the project, please sign up as a "project
observer" on the Codebase <https://json-xslt.codebase.ebay.com/> .
You'll need to be an eBay Developers Program member first, but signing
up <http://developer.ebay.com/join> for that is free. Once you sign up
for the project I'll reach out to you over email. I'm open to merging
this with Bram's project, and I'll be reaching out to him soon. I've
also started writing an article that will explain in painstaking detail
how to use this XSLT with eBay's web services.
<http://alanlewis.typepad.com/> Technical Evangelist
eBay Developers Program <http://developer.ebay.com/>
[Non-text portions of this message have been removed]