XSD to JSON Schema - Jsonix Schema Compiler

3,522 views
Skip to first unread message

Aleksei Valikov

unread,
May 2, 2015, 12:06:27 PM5/2/15
to json-...@googlegroups.com
Hi everyone,

I'm the author of Jsonix, a powerful XML<->JS mapping library. Jsonix also provides a Jsonix Schema Compiler which generates Jsonix mappings from XML Schema.

In the latest version of Jsonix Schema Compiler I've added a feature to generate JSON Schema from the original XML Schema alongside with Jsonix mappings.

So, effectively, Jsonix Schema Compiler can convert XML Schema into JSON Schema.

Let's take the Purchase Order schema for example. This is the JSON Schema the Jsonix Schema Compiler generates for it.

Why I'm writing this:
  • I'd like to ask for feedback and reviews. I'm rather new to the JSON Schema so I'm not sure if the generated JSON Schemas are the most optimal/following best practices.
  • I'd like to ask for a listing in the JSON software under "Schema generation" - whom should I contact for this? The tool is open-source with simplified BSD license.
  • Well, promote the project a bit as well.
It seems that there is a need for XSD->JSON Schema transition tools. I think my tool is very close to providing a pretty good solution.

Warning: this feature is just introduced in the version 2.3.4 of the Jsonix Schema Compiler, so it is in the experimental state. However I am pretty sure that generated JSON Schemas have quite a good quality and that JSON Schema generation will reach maturity pretty soon.

I'd love to hear what you think and if anyone would be interested to do some testing or PoCs.

Please feel free to ping me if you'll encounter any problems. I'm rather fanatic about support for my tools.

Best wishes,
Alexey

xmlbuddy

unread,
Aug 28, 2015, 7:27:25 AM8/28/15
to JSON Schema
Hi Alexey,

I tried to generate a JSON schema from a sample XSD (shiporder.xsd) but Jsonix didn't produce any output. I used the command-line in the following way:
java -jar jsonix-schema-compiler-full-2.3.6.jar -generateJsonSchema -logLevel DEBUG shiporder.xsd


And here is the input XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="shiporder">
<xs:annotation>
<xs:documentation>Shipping address</xs:documentation>
</xs:annotation>     
    <xs:complexType>
      <xs:sequence>
        <xs:element name="orderperson" type="xs:string"/>
        <xs:element name="shipto">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="address" type="xs:string"/>
              <xs:element name="city" type="xs:string"/>
              <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>       
      </xs:element>
        <xs:element maxOccurs="unbounded" name="item">
          <xs:complexType>
            <xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element minOccurs="0" name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal">
<xs:annotation>
<xs:documentation>Price of shipment</xs:documentation>
</xs:annotation>
</xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
      <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>


All I get is:
[DEBUG] org.hisrc.jsonix.configuration.ModulesConfiguration - Assigning id [generated] to the mapping with name [generated].
[DEBUG] org.hisrc.jsonix.configuration.MappingConfiguration - Package [generated] will be mapped by the mapping [generated].
[DEBUG] org.hisrc.jsonix.configuration.MappingConfiguration - Mapping [generated] will use "" as the target namespace as it is the most used element namespace URI in the package [generated].
[DEBUG] org.hisrc.jsonix.configuration.MappingConfiguration - Mapping [generated] will use "" as it is the most used attribute namespace URI in the package [generated].


But no JSON schema files are generated. Am I missing something? Any help is much appreciated :)

Kind regards
Clemens

Aleksei Valikov

unread,
Aug 28, 2015, 7:32:05 AM8/28/15
to json-...@googlegroups.com
Hi,

would you please file an issue:


Should actually work, probably something with the CLI.

Best wishes,
Alexey

--
You received this message because you are subscribed to a topic in the Google Groups "JSON Schema" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-schema/rZnWkUIbI1M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aleksei Valikov

unread,
Aug 30, 2015, 8:56:14 AM8/30/15
to json-...@googlegroups.com
Hi,

a small update on this.

I'm using the following CLI command to compile your schema:

java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar -generateJsonSchema -d mappings -b bindings -p ShipOrder -logLevel DEBUG schemas

Compilation succeeds, I'm getting ShipOrder.jsonschema in the mappings directory. Please check the following project which demonstrates compilation and validation:


I've also wrote a small guide on JSON Schema validation with AJV (https://github.com/epoberezkin/ajv) and JSON Schemas generated by the Jsonix Schema Compiler:


Best wishes,
Alexey

On Fri, Aug 28, 2015 at 1:27 PM, xmlbuddy <off...@xml-buddy.com> wrote:

--

xmlbuddy

unread,
Aug 31, 2015, 4:57:22 AM8/31/15
to JSON Schema
Hi Alexey,

the issue has been resolved now and I closed the related Github entry. The generation always worked, I just didn't set the output folder and was looking in the wrong folder for the generated schema...

Thanks a lot for taking a look!

Kind regards
Clemens

frank.ivens...@gmail.com

unread,
Sep 23, 2015, 5:49:01 AM9/23/15
to JSON Schema
Hello Aleksey

We are looking at using jsonix to generate json schema for XSDs that we already have, and I have a couple of questions. To explain our situation, we have been autogenerating XSD schemas for years, and our approach heavily relies on nested schemas, meaning lots of include statements. This is necessary for governance purposes, as structural schemas rely on other schema with shared common definitions. 

So my first question is this. It seems that your json schema generator inlines all nested schemas. Is there any way to make it output schemas nested with the same include structure as the source XSD schema?

The second issue is about output naming. All our XSD schemas provide definitions within one common namespace. It would appear, that your json schema output ends in one single file with a name derived from the namespace. I have an input XSD that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:fase2.1="http://skat.dk/begrebsmodel/2009/01/15/"
                 xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://skat.dk/begrebsmodel/2009/01/15/"
                 elementFormDefault="qualified"
                 attributeFormDefault="unqualified"
                 version="1.0">
         <xs:include schemaLocation="../types/SENummerType.xsd"/>
         <xs:include schemaLocation="../types/CVRNummerType.xsd"/>
         <xs:include schemaLocation="../types/DatoType.xsd"/>
         <xs:element name="VirksomhedSENummer" type="fase2.1:SENummerType">
               
               <xs:annotation>
                  <xs:documentation>8-cifret nummer,  der entydigt identificerer en registreret virksomhed i SKAT.</xs:documentation>
               </xs:annotation>
            </xs:element>
         <xs:element name="VirksomhedCVRNummer" type="fase2.1:CVRNummerType">
               
               <xs:annotation>
                  <xs:documentation>Det nummer der tildeles juridiske enheder i et Centralt Virksomheds Register (CVR).</xs:documentation>
               </xs:annotation>
            </xs:element>
         <xs:element name="VirksomhedStartDato" type="fase2.1:DatoType">
               
               <xs:annotation>
                  <xs:documentation>Angiver startdato for virksomheden</xs:documentation>
               </xs:annotation>
            </xs:element>
         <xs:element name="VirksomhedOphørDato" type="fase2.1:DatoType">
               
               <xs:annotation>
                  <xs:documentation>Angiver slutdato for virksomheden</xs:documentation>
               </xs:annotation>
            </xs:element>
      </xs:schema>


My generation command, which looks like this:

/usr/java/jre1.7.0_51/bin/java -jar ~/public_html/begrebsmodel/Mapping/jsonix-schema-compiler-full.jar -generateJsonSchema -d json -defaultNaming STANDARD Virksomhed.xsd

produces one output file in subfolder ./json with the name:

dk_skat_begrebsmodel__2009__01__15.jsonschemadk_skat_begrebsmodel__2009__01__15.jsonschema


and this file has all subschemas inlined:

{
    "anyOf":[
        {
            "type":"object",
            "properties":{
                "name":{
                },
                "value":{
                }
            },
            "elementName":{
                "localPart":"VirksomhedStartDato",
                "namespaceURI":"http://skat.dk/begrebsmodel/2012/01/01/"
            }
        },
        {
            "type":"object",
            "properties":{
                "name":{
                },
                "value":{
                }
            },
            "elementName":{
                "localPart":"VirksomhedSENummer",
                "namespaceURI":"http://skat.dk/begrebsmodel/2012/01/01/"
            }
        },
        {
            "type":"object",
            "properties":{
                "name":{
                },
                "value":{
                }
            },
            "elementName":{
                "localPart":"VirksomhedCVRNummer",
                "namespaceURI":"http://skat.dk/begrebsmodel/2012/01/01/"
            }
        },
        {
            "type":"object",
            "properties":{
                "name":{
                },
                "value":{
                }
            },
            "elementName":{
                "localPart":"VirksomhedOphørDato",
                "namespaceURI":"http://skat.dk/begrebsmodel/2012/01/01/"
            }
        }
    ]
}

My question is: Is it possible to have the output file name reflect the input file name, so I get "Virksomhed.jsonschema" instead of "dk_....."?

The third issue I have is with validation. As far as I can see, your package does not include a schema validation, so I've been looking for implementations of schema validators. Using the generated json schema shown above on this validator:

{
    "VirksomhedStartDato": "2015-09-15T09:30:00" ,
}


I get "success". However, the validator reports that "elementName" is undefined and thus ignored with this message:

[ {
  "level" : "warning",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/anyOf/0"
  },
  "domain" : "syntax",
  "message" : "the following keywords are unknown and will be ignored: [elementName]",
  "ignored" : [ "elementName" ]
},...


and I suppose this means that it will essentially not validate the names. So this structure also becomes valid:

{
    "HubbaBubba": "Chewing Gum"
}

Do you know of a validator (preferably in java) that will accept your generated json schema?

Thanks

Frank Carvalho


Aleksei Valikov

unread,
Sep 23, 2015, 6:27:37 AM9/23/15
to json-...@googlegroups.com
Hi Frank,

So my first question is this. It seems that your json schema generator inlines all nested schemas. Is there any way to make it output schemas nested with the same include structure as the source XSD schema?

At the moment there is no automatismus for includes. Jsonix Schema Compiler generates a separate module per namespace/package, but when you include schemas they share the same namespace.
You can configure it somewhat by defining modules which contain only specific elements or types (see https://github.com/highsource/jsonix-schema-compiler/wiki/Mapping-Configuration) but you'll basically need to manually structure your module/mapping configuration for you includes.

So, no, this won't easily work. Also probably not in the future because the underlying model only considers namespaces, not the physical structure of schemas (which schema includes which other schemas).

My question is: Is it possible to have the output file name reflect the input file name, so I get "Virksomhed.jsonschema" instead of "dk_....."?

Yes, you can do that using a configuration element like <jsonix:mapping name="Virksomhed" package="dk.skat..."/>.

See these examples:

* https://github.com/highsource/ogc-schemas/blob/master/arml/2.0/src/main/resources/arml-v_2_0.xjb - says the schema is getting a package net.opengis.arml.v_2_0
* https://github.com/highsource/ogc-schemas/blob/master/arml/2.0/src/main/resources/arml-v_2_0.jsonix.xjb - says the package net.opengis.arml.v_2_0 will be generated in the module ARML_2_0

So you'll get ARML_2_0.jsonschema generated

Do you know of a validator (preferably in java) that will accept your generated json schema?

I've also encountered this issue with validation. You are correct, element names are not checked at the moment (they must be). So if you have more than one root element, validation simply does not work (yet).

I am testing with AJV (https://github.com/epoberezkin/ajv), here's a sample test:


As I said, at the moment only works with a single root element, I have to generate constraints on element names additionally.

However, the validator reports that "elementName" is undefined and thus ignored with this message...

"elementName" and a few of other keywords are not JSON Schema standard keywords, just some metadata to describe the XML Schema counterpart. They are added for information purposes only, not supposed to be interpreted by the validator.

To put it very rough, the whole thing is still quite experimental and does not fully work yet.
But in a positive clue, what is generated is already quite good, just a few fragments missing.

Would you consider committing a test project in https://github.com/highsource/jsonix-support? I'd need your schemas and sample XMLs and could work towards providing more valid JSON Schema and samples for validation.

Best wishes,
Alexey

sh...@shawntalbert.com

unread,
Oct 23, 2015, 9:39:31 AM10/23/15
to JSON Schema
Jsonix is already great, this makes it even better. Thanks Alexey.


Reply all
Reply to author
Forward
0 new messages