[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Best options for converting JSON to XML

  • From: David Lee <dlee@calldei.com>
  • To: "Timothy W. Cook" <tim@mlhim.org>, "ihe.onwuka@g..."<ihe.onwuka@g...>
  • Date: Sun, 10 Nov 2013 14:59:31 +0000

RE:  Best options for converting JSON to XML
On Sun, Nov 10, 2013 at 6:15 AM, Ihe Onwuka <ihe.onwuka@gmail.com> wrote:
> Seeing as I just want to shell out to XML for validation I'd like to 
> think I don't have to care about what the converted XML looks like.
>

I suggest this is naive (no insult intended).

Why? One of the simplest and most common transformations of JSON to XML uses a fixed XML vocabulary
To deal with the problems of JSON names not all being valid XML QNames (as Michael mentioned in the first post),
As well as null values, preserving JSON types and arrays.
Thus the robust and simple way to solve this is to use an XML structure like (or some variation of)

{ "a json name" : 1 }                ->

<object>
    <member name="a json name">< value type="Number">123</value></<member>
</object>

This is very reliable but by its very nature is *always going to be schema valid* !!!
( atleast with XSD or RNG ... schematron might be able to do something with this)
Because the structure itself was designed to be schema valid no matter what JSON you throw at it.
All you are validating is the output XML structure, not the JSON data.
You could make up a new schema  but common schema vocabularies cant do much useful with the above, 
e.g. you cant easily indicate  

"For every member element with an attribute named 'name' with a value "a json name" it must have a single child 
element "value" with a "type" attribute  with the value "Number" and its text contents must follow the constraints of xs:decimal

This is difficult or impossible to express in common XML schema languages 
(I think you could do it in schematron though ... painfully,  maybe WC3 schema 1.1 with assertions ?)



So to convert to an XML structure which can be usefully validated with XML tools you need to preserve the JSON structure 
And do something insteead like say
   <a json name>1</a json name>

Ups. .. .thats not even valid XML ....

We'll have to do something a tad more complicated than that ... but what ?
(there are many solutions but few simple ones and certainly no single accepted one).

And now you've entered the world where just to do the basic stuff XML does with its deep ecosystem, is a PITA starting with JSON,
And its more subtle then you think to "just convert JSON to XML" to do it .  Because you *must* care about the XML conversion 
Or it wont accomplish what you want.

Maybe the JSON Schema initiative has gotten off the ground ? Last I checked there was a spec but no implementations.
It may have gotten better.


-David

----------------------------------------
David A. Lee
dlee@calldei.com
http://www.xmlsh.org







[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.