Does anybody know how to generate a JSON schema from a existing XML schema (XSD file)? Are there any tools available for this?
Plugin for converting an XML Schema (XSD) file to a JSON Schema file. Once installed, go to Tools -> XML Actions, or right-click on an XML Schema file from Project view, and select "Convert XSD to JSON Schema".
Result: The XSD to JSON Schema dialog box is now available and can be selected from the Tools > JSON Tools menu.
How to generate/create a schema xsd from an XML document? Step 1: click Open File button and select the xml file from the file system that you have access, or get the xml file from internet via URL, click By URL. Step 2: click the Generate XSD button, the generated schema will be displayed in an indented XML format.
Jsonix (JSON interfaces for XML) is a JavaScript library which allows you to convert between XML and JSON structures. With Jsonix you can parse XML into JSON (this process is called unmarshalling) or serialize JSON in XML form (this is called marshalling).
Disclaimer: I am the author of Jsonix, a powerful open-source XML<->JSON 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:
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType>
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 XML<->JSON 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.
Links:
npm install
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With