Converting XSD ComplexType to JSON Schema
Understanding how the workhorse of XML Schema—the complexType—maps to the world of JSON Schema
objects and properties.
What is complexType?
In XML Schema (XSD), a complexType defines an element that can contain attributes and other
elements. It is strictly hierarchical.
In JSON Schema, this concept maps almost directly to an object definition with a properties
keyword. However, handling order (`sequence`) and optionality (`choice`) requires specific techniques.
Mapping complexType to "type": "object"
The most direct translation involves setting the JSON type to "object" and enumerating children in "properties".
The Mapping Pattern
- XSD Name → JSON Property Key
- XSD Type → JSON Schema Type (e.g., xs:string → string)
- minOccurs="1" → Added to "required" array
- minOccurs="0" → Omitted from "required" array
Handling <xs:sequence>
XML is an ordered format; JSON objects are technically unordered (though most parsers preserve insertion order). Standard conversion ignores the strict ordering enforcement of `sequence` in favor of a flat property list, as this is more idiomatic for JSON APIs.
Handling <xs:choice>
This is a trickier pattern. An XSD `choice` means "one of these elements must exist".
XSD Choice Input
<xs:choice> <xs:element name="error" type="xs:string"/> <xs:element name="result" type="xs:int"/> </xs:choice>
JSON Schema Approach
Our converter typically maps this using the oneOf keyword to ensure validation logic remains
strictly equivalent:
"oneOf": [
{ "required": ["error"] },
{ "required": ["result"] }
]
Automate Your Complex Conversions
Don't map these manually. Our tool handles complexTypes, sequences, and choices instantly.
Use XSD to JSON Schema Converter