Convert XSD to JSON Schema

What an XSD to JSON Schema conversion keeps, what it loses, and how to check whether the generated schema is reliable enough for your validation work.

Converting an XML Schema Definition (XSD) into JSON Schema is useful when data that was modeled for XML now travels as JSON, for example in a REST API that replaced a SOAP or file-based interface. A converter can carry over much of the structure and many of the value constraints. It cannot carry over everything, because the two schema languages describe different data models. This review explains what changes during conversion, where meaning is lost or weakened, and how to decide whether a generated schema is fit for your use.

Evidence note: no specific converter, version or test result was supplied for this review. The points below rest on documented behavior of the XSD and JSON Schema specifications and on general principles. They are not measurements of any particular tool, and any tool you choose should be checked against your own schemas.

Is converting XSD to JSON Schema suitable for your use?

Bounded recommendation: conversion is a sound starting point when your XSD mainly uses elements, attributes, simple-type restrictions, optional and repeated fields, and straightforward type extension. The main benefit is speed: you get a draft schema that mirrors an existing, often carefully reviewed data contract, instead of rewriting it by hand.

The decisive tradeoff is fidelity. Element order, namespaces, identity constraints such as keys and key references, substitution groups and mixed content have no direct JSON Schema equivalent. A converter must drop, approximate or flatten them, and the output can look complete while validating less than the original XSD did.

Keep the XSD, or write a JSON Schema by hand, when any of the following applies: the data still needs to be validated as XML; the schema relies heavily on keys, namespaces or ordering for correctness; you need a readable schema that people will maintain, rather than generated output; or the JSON shape you actually publish was designed independently and does not follow a mechanical XML-to-JSON mapping.

Which conversion does "XSD to JSON" mean?

The phrase is used for three different tasks, and tools are not always clear about which one they perform:

  • Schema to schema: turning an XSD into a JSON Schema document that validates JSON data. This is the subject of this article.
  • Instance to instance: turning an XML document into a JSON document. This involves no schema output at all, although some tools use an XSD to decide types and arrays.
  • Schema to sample: generating an example JSON payload from an XSD, which is useful for documentation and mocking but validates nothing.

The first two are linked. A converted JSON Schema only works if the JSON documents you validate follow the same mapping convention the converter assumed, for instance whether attributes become properties with an "@" prefix, where element text goes in an element that also has attributes, and whether repeatable elements are always arrays.

What the conversion actually changes

Unlike a text transformation that rearranges characters or restyles their appearance, this conversion rewrites one set of rules into another rule language. Nothing about your data changes; what changes is which data counts as valid. XSD describes trees of named, ordered, namespace-qualified elements with attributes. JSON Schema describes objects with unordered named properties, arrays and a small set of primitive types. Every XSD construct has to be mapped onto that smaller model.

The illustration below is hand-written to show a typical mapping. It is not output from any specific tool, and the namespace declaration on the XSD root and the $schema keyword in the JSON Schema are omitted for brevity. It assumes attributes are mapped to properties prefixed with "@".

<xs:element name="order">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="sku" 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>
{
  "type": "object",
  "properties": {
    "order": {
      "type": "object",
      "properties": {
        "@id": { "type": "integer", "minimum": 1 },
        "sku": {
          "type": "array",
          "items": { "type": "string" },
          "minItems": 1
        },
        "note": { "type": "string" }
      },
      "required": ["@id", "sku"],
      "additionalProperties": false
    }
  },
  "required": ["order"]
}

Checked against the mechanism: the required attribute becomes a required property; xs:positiveInteger becomes an integer with a minimum of 1; the repeatable sku element, which by default must appear at least once, becomes an array with at least one item; and the optional note is left out of the required list. One rule is gone: the XSD requires sku elements to come before note, while the JSON Schema has no notion of property order. Another is weakened: xs:positiveInteger has no upper limit, but many JSON parsers read numbers as double-precision floating point and lose precision on very large integers.

How common XSD constructs map

XSD construct | Usual JSON Schema approach | Fidelity note
xs:sequence (element order) | properties of an object | Order is not enforced
xs:choice | oneOf or anyOf | Workable; oneOf is exclusive, anyOf is not
minOccurs / maxOccurs | required; arrays with minItems / maxItems | Depends on whether single occurrences are also arrays in the JSON data
Attributes | Properties, often with a name prefix | Without a prefix, attribute and child names can collide
xs:extension | allOf with $ref | Combining with additionalProperties: false is error-prone; later drafts offer unevaluatedProperties
enumeration, length and range facets | enum, minLength / maxLength, minimum / maximum | Generally direct
totalDigits / fractionDigits | No direct equivalent; multipleOf can approximate fractionDigits | Often dropped
xs:pattern | pattern | Regex dialects differ; XSD patterns are implicitly anchored, JSON Schema patterns are not
xs:date, xs:dateTime and similar | string with format | Many validators treat format as an annotation unless assertion is enabled
nillable | Adding null to the allowed types | Depends on how nil is represented in the JSON
default / fixed | default / const | JSON Schema validators do not insert defaults
xs:key, xs:keyref, xs:unique | None | Lost; needs application-level checks
Substitution groups, xs:redefine | None directly | Usually flattened or dropped
Namespaces | None | Prefixes are dropped or folded into property names
Mixed content | None directly | Approximated at best
xs:annotation / xs:documentation | description | Usually preserved

The pattern row deserves special attention because it fails silently. An XSD pattern such as [A-Z]{3} must match the whole value. Copied unchanged into JSON Schema, the same pattern matches any string that contains three capital letters anywhere. A careful converter wraps the pattern in ^ and $. XSD-specific escapes such as \i and \c, and character class subtraction, have no ECMAScript equivalent and need manual rewriting.

Will the generated schema stay usable after copying or sharing?

The output is plain JSON text, so copying and pasting it does not alter it the way styled Unicode text can be altered. The practical limits come from how the schema is structured and which validators will read it.

  • Draft version: JSON Schema has several published drafts. The $schema keyword declares which one applies, and validators differ in which drafts they support. Reusable definitions live under definitions in draft-07 and under $defs in 2019-09 and later, so a schema written for one draft may not resolve correctly under another.
  • References across files: an XSD that uses xs:include or xs:import may become several JSON Schema files linked by $ref. Sharing one of those files on its own, or moving it to a different folder, breaks relative references.
  • Readability and editing: generated schemas tend to be verbose, with deeply nested or mechanically named definitions. If people will maintain the schema, expect to spend time renaming and restructuring it, and decide whether the XSD or the JSON Schema is the source of truth so the two do not drift apart.
  • Documentation: carried-over description fields help anyone reading the schema or generating API documentation from it, but only if the converter preserved xs:documentation.
  • Sensitive schemas: before pasting a proprietary or confidential XSD into an online converter, read that tool's stated data handling policy. The appearance of a tool says nothing reliable about what it stores.

Accessibility concerns that apply to visual text effects, such as screen readers misreading substituted characters, do not arise here; the schema is ordinary text. What matters for downstream users is that the schema is valid for its declared draft and accepts the JSON your system actually produces.

How to check a converted schema before relying on it

  • Read the converter's documentation to find out which XML-to-JSON mapping convention it assumes, and confirm your JSON producer uses the same one.
  • Validate the generated schema itself against its declared draft's metaschema.
  • Take XML samples that pass and fail the original XSD, convert them to JSON with your real pipeline, and confirm the JSON Schema gives the same verdicts.
  • Include edge cases: a repeatable element that appears once, an empty optional element, a nil value, a value that only partly matches a pattern, and very large numbers.
  • List every XSD construct from the table above that has no equivalent, and decide where those rules will be enforced instead.

Test results from one validator or one set of samples apply to that setup only. If the schema will be used by several systems, check it with the validators those systems actually run.

Gaps in this review

No particular converter was named or tested, so this review cannot say how any given tool handles patterns, choices, extensions or multi-file schemas, which draft it targets, or how it treats uploaded data. Those questions should be answered from the tool's own documentation and from round-trip tests on your schemas.