4

Of course XML and JSON are different. But if you see these formats on the level of pure structure they might be exchangeable representations for the same data. Both can be described with schema definitions: JSON Schema and XML Schema.

Can you describe the same data structures with JSON Schema as you can describe with XML Schema and vise versa? Is it possible to write JSSD/XSD converters that don't loose information while converting?

2

2 Answers 2

3

I'm pretty sure the answer is no.

Firstly, it's quite hard to define a completely lossless mapping between JSON and XML at the instance level even before you start thinking about schemas. So the concept of "the same data structures" needs qualification.

Much of XML Schema is concerned with grammars and content models, for example saying that a section consists of an optional heading followed by zero or more paragraphs, each of which can be a p, ul, or table element. I don't think there's anything remotely like that in JSON Schema. Arrays in JSON Schema seem to be treated as uniform and homogenous.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure about the correctness of your premise. XML is nothing else than a treelike structure and so is JSON. The only things in XML that don't exist in JSON are namespaces and the specification of attributes. Both can be mapped to JSON (e.g. using qualifiers for attributes like $attribute as well as strings instead of namespace notation like "p-parameter": CONTENT instead of <p:parameter> CONTENT </p:parameter>). When talking about schema description it might be a different thing because here we don't see structure, but semantic.
I find it ironic that JavaScript is giving rise to Type-Safe variants (e.g. TypeScript) and now, finally, JSON schemas are emerging to solve the serialization/deserialization problem where 'grammar', domain integrity and type safety make a difference. We all agree an XML document that has not been validated against a strict schema is easily replaced by a lean (not fat) JSON document. Schemas assert whether a "1" is a number (floating or integral) or is a string; and in some cases that is important to know.
JSON schema doesn't provide type safety, it can only assure that a given JSON payload is structurally conformant according to a provided schema. That's why people get tripped up by the lack of "extension" mechanism in JSON schema they way it's done in their language of choice.
"We all agree an XML document that has not been validated against a strict schema is easily replaced [by JSON]" - Only if it doesn't use mixed content.
3

I'm pretty sure the answer is yes in a while.

Using current JSON Schema draft 04 (hyperschema) you can currently map the most of XML Schema to JSON Schema. There are some ugly workarounds which will be obsolete with draft 05: I am pretty sure that the proposals propertyLinks will "make it" into the next draft, see this page: https://github.com/json-schema/json-schema/wiki/v5-Proposals

There are also existing projects, e.g. owl2vowl and webowl is using a to JSON Schema converter. It is Java. I am not sure how far the XML Schema support is. However:

I began writing a JSON Schema "playground" in javascript (with an optional 'node.js part'). In javascript there is a solution for schema.org. And I asked myself how to import OWL / RDFS / XML Schema in a similar manner. I am 70% ready with the OWL / RDFS part now.

So if your interested in this, we could setup a discussion for the XML Schema part and I could push to github before.

1 Comment

Do you have any solution for converting Json schema to XSD? I'm struggling in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.