Generate Json schema from XML schema (XSD) [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago .

Does anybody know how to generate a JSON schema from a existing XML schema (XSD file)? Are there any tools available for this?

7,008 7 7 gold badges 55 55 silver badges 79 79 bronze badges asked Oct 13, 2010 at 8:47 JB Hurteaux JB Hurteaux 4,528 6 6 gold badges 33 33 silver badges 35 35 bronze badges

I think the real question, "Is it possible to make a mapping between JSON Schema and XML Schema?" is not off-topic. Perhaps the question could be reworded.

Commented Aug 15, 2014 at 19:34 Falco Nogatz did a BSc thesis in 2013 resulting in xsd2json. Commented Nov 4, 2014 at 18:01

One way is to go from XSD to Java classes and from Java classes to JSON schema. Details at dzone.com/articles/generating-json-schema-xsd.

Commented Jun 13, 2018 at 13:25

5 Answers 5

Disclaimer: I am the author of Jsonix, a powerful open-source XMLJSON JavaScript mapping library.

Today I've released the new version of the Jsonix Schema Compiler, with the new JSON Schema generation feature.

Let's take the Purchase Order schema for example. Here's a fragment:

You can compile this schema using the provided command-line tool:

java -jar jsonix-schema-compiler-full.jar -generateJsonSchema -p PO schemas/purchaseorder.xsd 

The compiler generates Jsonix mappings as well the matching JSON Schema.

Here's what the result looks like (edited for brevity):

 < "id":"PurchaseOrder.jsonschema#", "definitions":< "PurchaseOrderType":< "type":"object", "title":"PurchaseOrderType", "properties":< "shipTo":< "title":"shipTo", "allOf":[ < "$ref":"#/definitions/USAddress" >] >, "billTo": < "title":"billTo", "allOf":[ < "$ref":"#/definitions/USAddress" >] >, . > >, "USAddress":< . >, . >, "anyOf":[ < "type":"object", "properties":< "name":< "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName" >, "value": < "$ref":"#/definitions/PurchaseOrderType" >>, "elementName": < "localPart":"purchaseOrder", "namespaceURI":"" >> ] > 

Now this JSON Schema is derived from the original XML Schema. It is not exactly 1:1 transformation, but very very close.

The generated JSON Schema matches the generatd Jsonix mappings. So if you use Jsonix for XMLJSON conversion, you should be able to validate JSON with the generated JSON Schema. It also contains all the required metadata from the originating XML Schema (like element, attribute and type names).

Disclaimer: At the moment this is a new and experimental feature. There are certain known limitations and missing functionality. But I'm expecting this to manifest and mature very fast.

answered May 2, 2015 at 20:13 43.5k 17 17 gold badges 136 136 silver badges 225 225 bronze badges JsonSchema is dead. Any plans to support openAPI? Commented Jun 26, 2018 at 15:07

@Lonzak No plans so far. But from the first glance on the OpenAPI spec, don't schema parts follow the JSON Schema spec?

Commented Jun 26, 2018 at 16:27 this does not work, for example, with Java 12 Commented Nov 13, 2019 at 10:24 @Lonzak JSON Schema is used by OpenAPI to describe the shape of JSON content. Commented Mar 5, 2022 at 19:48

@EricHartford Has been a while :-) You are right. OpenAPI is using an older draft of json schema which is still in its draft phase today (2022).

Commented Mar 7, 2022 at 7:57

JSON Schema is not intended to be feature equivalent with XML Schema. There are features in one but not in the other.

In general you can create a mapping from XML to JSON and back again, but that is not the case for XML schema and JSON schema.

That said, if you have mapped a XML file to JSON, it is quite possible to craft an JSON Schema that validates that JSON in nearly the same way that the XSD validates the XML. But it isn't a direct mapping. And it is not possible to guarantee that it will validate the JSON exactly the same as the XSD validates the XML.

For this reason, and unless the two specs are made to be 100% feature compatible, migrating a validation system from XML/XSD to JSON/JSON Schema will require human intervention.