Convert XSD to JSON Schema
What converting XSD to JSON Schema preserves, what it loses, and how to judge whether a generated schema fits your validation needs or needs manual work.
Converting an XML Schema Definition (XSD) into JSON Schema looks like a simple format swap, but the two languages describe different data models. A converter can translate many structural rules automatically. Some XSD features have no direct JSON Schema equivalent, and those must be approximated, dropped, or handled outside the schema. This review explains what such a conversion actually changes, where meaning tends to slip, and when hand-writing or adjusting the schema is the better choice.
A note on evidence: no specific converter, test results, or measurements were supplied for this article. The points below rest on the documented behavior of the XSD and JSON Schema languages, not on a hands-on comparison of tools. Where a claim depends on a particular tool or validator, that is flagged as something you should check yourself.
Is converting XSD to JSON Schema suitable for your use?
Automated conversion is a reasonable starting point when you are moving an existing XML-based contract toward JSON and want a draft that captures types, required fields, enumerations, and nesting without retyping them. The main benefit is speed and consistency on the parts of the schema that map cleanly.
The decisive tradeoff is that a JSON Schema only makes sense relative to a specific way of turning XML documents into JSON. XML has attributes, element order, mixed text and markup, and namespaces; JSON has none of these natively. Every converter has to pick a convention, such as prefixing attribute names, wrapping repeated elements in arrays, or storing text content under a reserved key. If the JSON your systems actually produce follows a different convention, the generated schema will reject valid data or accept invalid data.
Plan to review and edit the output rather than use it as-is when any of the following apply:
- Your XSD relies on identity constraints (xs:key, xs:keyref, xs:unique), which JSON Schema cannot express.
- Element order carries meaning, because JSON object properties are unordered.
- You use mixed content, where text and child elements are interleaved.
- The XSD uses regular-expression patterns, substitution groups, or deep type derivation.
- You are designing a new JSON API rather than mirroring an XML one; a schema written for idiomatic JSON is usually cleaner than one shaped by XML conventions.
Keeping the XSD as the source of truth, and validating XML directly, is preferable when your data stays in XML. Converting the schema only pays off if JSON documents will actually be validated against it.
What the conversion actually changes
Converting a schema is a different job from converting data, and the two are often confused. Converting XML documents to JSON changes the data itself. Converting an XSD to JSON Schema changes only the rules: it produces a JSON document describing what valid JSON instances should look like. The phrase JSON Schema also has two common meanings. Strictly, it refers to the JSON Schema specification and its vocabulary (type, properties, required, items, and so on). Loosely, people use it for any schema written in JSON. Converters target the former, and they target a particular draft of it.
The table below summarizes typical mappings. Exact output varies by converter, so treat it as a description of what is possible in each language, not of any specific tool.
| XSD construct | Usual JSON Schema approximation | What may be lost or changed |
| xs:complexType with xs:sequence | object with properties and required | Element order is not enforced |
| xs:choice | oneOf or anyOf | Interaction with repetition can become hard to read |
| minOccurs / maxOccurs | required, or array with minItems / maxItems | Whether a single occurrence becomes a value or a one-item array depends on the data mapping |
| xs:attribute | property, often with a prefix such as @ | The attribute/element distinction exists only by naming convention |
| xs:enumeration | enum | Usually preserved |
| xs:pattern | pattern | Regex dialects differ; XSD patterns are implicitly anchored, JSON Schema patterns are not |
| minInclusive / maxInclusive | minimum / maximum | Usually preserved for numeric types |
| totalDigits / fractionDigits | partial approximation (for example multipleOf) or none | Decimal precision rules are weakened |
| xs:dateTime, xs:date | string with format | format is an annotation by default in newer drafts, so it may not be enforced |
| Type derivation by extension | allOf combining a base schema | Readability drops; behavior with additionalProperties needs care |
| xs:any | additionalProperties or unevaluatedProperties | Namespace restrictions are lost |
| xs:import / xs:include | $ref to separate schema files or definitions | Namespaces have no JSON equivalent |
| nillable | type including null | Depends on how the data mapping represents nil |
| default / fixed | default / const | default is informational and does not fill in values |
| xs:key, xs:keyref, xs:unique | no equivalent | Must be checked in application code |
A worked example
Here is a small XSD element: an order with a required numeric id attribute, one or more items, and an optional note.
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>
Assuming a data mapping that prefixes attributes with @ and always represents repeatable elements as arrays, one reasonable JSON Schema translation is:
{
"type": "object",
"properties": {
"order": {
"type": "object",
"properties": {
"@id": { "type": "integer", "minimum": 1 },
"item": {
"type": "array",
"items": { "type": "string" },
"minItems": 1
},
"note": { "type": "string" }
},
"required": ["@id", "item"],
"additionalProperties": false
}
},
"required": ["order"]
}
Checking this output against the source shows what moved and what did not. The required attribute and the at-least-one item rule survive. The rule that item must come before note does not, because JSON properties have no order. The schema expects @id to be a JSON number; if your XML-to-JSON step leaves attribute values as strings, valid documents will fail. xs:positiveInteger has no upper bound, while many JSON parsers lose precision on very large numbers, so extreme values may not round-trip. The schema also omits a $schema declaration, which you should add to state which JSON Schema draft it targets; the example uses keywords that are common to recent drafts.
Will the result stay readable and usable after copying or sharing?
The output is ordinary JSON text, so copying, pasting, and version control work as with any source file. It involves no special Unicode characters or visual styling, and screen readers and editors handle it like any other code. The practical usability limits come from compatibility, not from the text itself.
- Draft version: JSON Schema has several drafts with differing keywords and semantics. Confirm which draft the converter emits and which drafts your validator supports; mismatches can cause keywords to be silently ignored.
- format enforcement: whether date, date-time, and similar formats are actually validated depends on the validator and its configuration. Check your validator's documentation rather than assuming.
- Regular expressions: JSON Schema patterns are meant to follow ECMA-262 regex syntax, while XSD has its own dialect. Patterns using XSD-specific character classes may fail to compile or may match differently. Review every converted pattern, and add ^ and $ anchors where the XSD relied on implicit anchoring.
- Readability for maintainers: generated schemas often contain deep allOf chains and convention-driven property names. If people will edit the schema by hand, refactoring it into named definitions referenced with $ref usually helps.
- Documentation: xs:annotation and xs:documentation content may or may not be carried into description fields. Check whether this matters for anyone reading the schema later.
If you use an online converter, its interface does not tell you how uploaded schemas are stored or logged. For schemas that describe internal or confidential systems, check the provider's stated data handling terms, or use a tool that runs locally.
How to check a converted schema
Before relying on a generated schema, verify it against real data rather than judging it by appearance:
- Take sample XML documents that validate against the original XSD, convert them to JSON with the exact mapping your systems use, and validate them against the new schema. Every one should pass.
- Take documents known to violate the XSD, convert them, and confirm the JSON Schema rejects them. Expect some to pass where the violation involved order, identity constraints, or other unsupported rules, and decide where those checks will live instead.
- Include edge cases: a repeatable element that appears once, empty or nil values, optional attributes that are absent, and boundary numeric values.
- Run the schema through the validator you will use in production, set to the draft you intend to target.
Bottom line
Converting XSD to JSON Schema reliably carries over types, required fields, enumerations, numeric ranges, and nesting. It cannot carry over element order, identity constraints, namespaces, or precise decimal rules, and its correctness depends on matching the XML-to-JSON mapping your data actually uses. Treat automated output as a draft to test and refine. When you are designing a JSON format from scratch, writing the schema directly for JSON is often simpler than inheriting XML's structure.