jsonxmlxsdjsonschemajsonix

Convert xsd to json schema (Jsonix, XMLSpy)


I am trying to convert xml schema to json schema.

In some reason the json schema that I get is weird, and incorrect. I use Jsonix and Altova XMLSpy 2017

For instance, this is the given xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://jaxb.vo.dozer.org/Employee" xmlns:tns="http://jaxb.vo.dozer.org/Employee">
  <complexType name="EmployeeType">
    <sequence>
      <element name="FirstName" type="string" minOccurs="1" maxOccurs="1"></element>
      <element name="BirthDate" type="date" minOccurs="0" maxOccurs="1"></element>
      <element name="LastName" type="string" minOccurs="1" maxOccurs="1"></element>
    </sequence>
  </complexType>

  <element name="Employee" type="tns:EmployeeType"></element>
  <element name="EmployeeWithInnerClass">
    <complexType>
      <sequence>
        <element name="FirstName" type="string" minOccurs="1" maxOccurs="1"></element>
        <element name="BirthDate" type="date" minOccurs="0" maxOccurs="1"></element>
        <element name="LastName" type="string" minOccurs="1" maxOccurs="1"></element>
        <element name="Address">
          <complexType>
            <sequence>
              <element name="Street" type="string"></element>
            </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

And this is the json schema that I got from Jsonix :

var org_dozer_vo_jaxb_employee_Module_Factory = function () {
  var org_dozer_vo_jaxb_employee = {
    name: 'org_dozer_vo_jaxb_employee',
    typeInfos: [{
        localName: 'EmployeeType',
        typeName: {
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee',
          localPart: 'EmployeeType'
        },
        propertyInfos: [{
            name: 'firstName',
            required: true,
            elementName: {
              localPart: 'FirstName'
            }
          }, {
            name: 'birthDate',
            elementName: {
              localPart: 'BirthDate'
            },
            typeInfo: 'Date'
          }, {
            name: 'lastName',
            required: true,
            elementName: {
              localPart: 'LastName'
            }
          }]
      }, {
        localName: 'EmployeeWithInnerClass.Address',
        typeName: null,
        propertyInfos: [{
            name: 'street',
            required: true,
            elementName: {
              localPart: 'Street'
            }
          }]
      }, {
        localName: 'EmployeeWithInnerClass',
        typeName: null,
        propertyInfos: [{
            name: 'firstName',
            required: true,
            elementName: {
              localPart: 'FirstName'
            }
          }, {
            name: 'birthDate',
            elementName: {
              localPart: 'BirthDate'
            },
            typeInfo: 'Date'
          }, {
            name: 'lastName',
            required: true,
            elementName: {
              localPart: 'LastName'
            }
          }, {
            name: 'address',
            required: true,
            elementName: {
              localPart: 'Address'
            },
            typeInfo: '.EmployeeWithInnerClass.Address'
          }]
      }],
    elementInfos: [{
        typeInfo: '.EmployeeWithInnerClass',
        elementName: {
          localPart: 'EmployeeWithInnerClass',
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee'
        }
      }, {
        typeInfo: '.EmployeeType',
        elementName: {
          localPart: 'Employee',
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee'
        }
      }]
  };
  return {
    org_dozer_vo_jaxb_employee: org_dozer_vo_jaxb_employee
  };
};
if (typeof define === 'function' && define.amd) {
  define([], org_dozer_vo_jaxb_employee_Module_Factory);
}
else {
  var org_dozer_vo_jaxb_employee_Module = org_dozer_vo_jaxb_employee_Module_Factory();
  if (typeof module !== 'undefined' && module.exports) {
    module.exports.org_dozer_vo_jaxb_employee = org_dozer_vo_jaxb_employee_Module.org_dozer_vo_jaxb_employee;
  }
  else {
    var org_dozer_vo_jaxb_employee = org_dozer_vo_jaxb_employee_Module.org_dozer_vo_jaxb_employee;
  }
}

why I got all this functions there? I did something worng?

With Altova XMLSpy the result is different at all:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "additionalProperties": false,
    "definitions": {
        ".tns:Employee": {
            "$ref": "#/definitions/tns:EmployeeType"
        },
        ".tns:EmployeeWithInnerClass": {
            "additionalProperties": false,
            "patternProperties": {
                "^\\?\\w+$": {
                    "type": "string"
                }
            },
            "properties": {
                "Address": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "Street": {
                            "additionalProperties": false,
                            "patternProperties": {
                                "^\\?\\w+$": {
                                    "type": "string"
                                }
                            },
                            "properties": {
                                "$": {
                                    "$ref": "#/definitions/xs:string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "required": [
                        "Street"
                    ],
                    "type": "object"
                },
                "BirthDate": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:date"
                        }
                    },
                    "type": "object"
                },
                "FirstName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                },
                "LastName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                }
            },
            "required": [
                "Address",
                "FirstName",
                "LastName"
            ],
            "type": "object"
        },
        "tns:EmployeeType": {
            "additionalProperties": false,
            "patternProperties": {
                "^\\?\\w+$": {
                    "type": "string"
                }
            },
            "properties": {
                "BirthDate": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:date"
                        }
                    },
                    "type": "object"
                },
                "FirstName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                },
                "LastName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                }
            },
            "required": [
                "FirstName",
                "LastName"
            ],
            "type": "object"
        },
        "xs:date": {
            "type": "string"
        },
        "xs:string": {
            "type": "string"
        }
    },
    "description": "JSON Schema generated by XMLSpy v2017 sp1 (x64) (http://www.altova.com)",
    "patternProperties": {
        "^\\?\\w+$": {
            "type": "string"
        }
    },
    "properties": {
        "@xmlns:tns": {
            "default": "http://jaxb.vo.dozer.org/Employee"
        },
        "@xmlns:xs": {
            "default": "http://www.w3.org/2001/XMLSchema"
        },
        "tns:Employee": {
            "$ref": "#/definitions/.tns:Employee"
        },
        "tns:EmployeeWithInnerClass": {
            "$ref": "#/definitions/.tns:EmployeeWithInnerClass"
        }
    },
    "type": "object"
}

Thanks


Solution

  • I'm the author of Jsonix.

    Jsonix output you've posted is not the JSON Schema, it is Jsonix mappings for your schema. If you want to generate JSON Schema, add the -generateJsonSchema parameter. See here.

    Here's what Jsonix would generate for your schema:

    {
        "id":"#",
        "definitions":{
            "EmployeeWithInnerClass":{
                "type":"object",
                "title":"EmployeeWithInnerClass",
                "required":[
                    "firstName",
                    "lastName",
                    "address"
                ],
                "properties":{
                    "firstName":{
                        "title":"firstName",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"FirstName",
                            "namespaceURI":""
                        }
                    },
                    "birthDate":{
                        "title":"birthDate",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/date"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"BirthDate",
                            "namespaceURI":""
                        }
                    },
                    "lastName":{
                        "title":"lastName",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"LastName",
                            "namespaceURI":""
                        }
                    },
                    "address":{
                        "title":"address",
                        "allOf":[
                            {
                                "$ref":"#/definitions/EmployeeWithInnerClass.Address"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"Address",
                            "namespaceURI":""
                        }
                    }
                },
                "typeType":"classInfo",
                "propertiesOrder":[
                    "firstName",
                    "birthDate",
                    "lastName",
                    "address"
                ]
            },
            "EmployeeWithInnerClass.Address":{
                "type":"object",
                "title":"EmployeeWithInnerClass.Address",
                "required":[
                    "street"
                ],
                "properties":{
                    "street":{
                        "title":"street",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"Street",
                            "namespaceURI":""
                        }
                    }
                },
                "typeType":"classInfo",
                "propertiesOrder":[
                    "street"
                ]
            },
            "EmployeeType":{
                "type":"object",
                "title":"EmployeeType",
                "required":[
                    "firstName",
                    "lastName"
                ],
                "properties":{
                    "firstName":{
                        "title":"firstName",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"FirstName",
                            "namespaceURI":""
                        }
                    },
                    "birthDate":{
                        "title":"birthDate",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/date"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"BirthDate",
                            "namespaceURI":""
                        }
                    },
                    "lastName":{
                        "title":"lastName",
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                            }
                        ],
                        "propertyType":"element",
                        "elementName":{
                            "localPart":"LastName",
                            "namespaceURI":""
                        }
                    }
                },
                "typeType":"classInfo",
                "typeName":{
                    "localPart":"EmployeeType",
                    "namespaceURI":"http://jaxb.vo.dozer.org/Employee"
                },
                "propertiesOrder":[
                    "firstName",
                    "birthDate",
                    "lastName"
                ]
            }
        },
        "anyOf":[
            {
                "type":"object",
                "properties":{
                    "name":{
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName"
                            },
                            {
                                "type":"object",
                                "properties":{
                                    "localPart":{
                                        "enum":[
                                            "Employee"
                                        ]
                                    },
                                    "namespaceURI":{
                                        "enum":[
                                            "http://jaxb.vo.dozer.org/Employee"
                                        ]
                                    }
                                }
                            }
                        ]
                    },
                    "value":{
                        "$ref":"#/definitions/EmployeeType"
                    }
                },
                "elementName":{
                    "localPart":"Employee",
                    "namespaceURI":"http://jaxb.vo.dozer.org/Employee"
                }
            },
            {
                "type":"object",
                "properties":{
                    "name":{
                        "allOf":[
                            {
                                "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName"
                            },
                            {
                                "type":"object",
                                "properties":{
                                    "localPart":{
                                        "enum":[
                                            "EmployeeWithInnerClass"
                                        ]
                                    },
                                    "namespaceURI":{
                                        "enum":[
                                            "http://jaxb.vo.dozer.org/Employee"
                                        ]
                                    }
                                }
                            }
                        ]
                    },
                    "value":{
                        "$ref":"#/definitions/EmployeeWithInnerClass"
                    }
                },
                "elementName":{
                    "localPart":"EmployeeWithInnerClass",
                    "namespaceURI":"http://jaxb.vo.dozer.org/Employee"
                }
            }
        ]
    }