XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
RE: [xml-dev] 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]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS