XML to JSON Schema: describing XML data as JSON
Many systems receive XML but store or forward it as JSON: integration platforms, API gateways and message queues often convert between the two. This XML to JSON Schema generator infers a schema for that JSON form. It first maps your XML to objects (attributes and child elements become properties, element text beside attributes becomes value, repeated elements become arrays) and then derives a draft-06 JSON Schema from the result.
Example
<invoice number="INV-2026-001" currency="EUR">
<customer>Acme GmbH</customer>
<line sku="A1" qty="2">12.50</line>
<line sku="B7" qty="1">99.00</line>
<paid>false</paid>
</invoice>
"Invoice": {
"type": "object",
"additionalProperties": false,
"properties": {
"customer": { "type": "string" },
"line": { "type": "array", "items": { "$ref": "#/definitions/Line" } },
"paid": { "type": "boolean" },
"number": { "type": "string" },
"currency": { "type": "string" }
},
"required": ["currency", "customer", "line", "number", "paid"]
},
"Line": {
"properties": {
"value": { "type": "number" },
"sku": { "type": "string" },
"qty": { "type": "integer" }
}
}
The amount in each <line> is element text next to attributes, so it appears as value; 12.50 was recognized as a number and qty as an integer. The schema describes JSON, so the distinction between attributes and elements is not preserved.
JSON Schema vs. XSD
If you need to validate the XML itself, the native tool is an XML Schema (XSD) or a RELAX NG schema, which can express attributes, element order, namespaces and mixed content. JSON Schema cannot describe those XML-specific features, but it is the better fit when the data will travel as JSON after conversion:
| XSD | JSON Schema from this tool | |
|---|---|---|
| Validates | The XML document | The JSON form of the data |
| Attributes vs. elements | Distinguished | Both become properties |
| Element order | Can be enforced | Not represented |
| Namespaces | Supported | Prefixes removed |
Review before use
- Text that looks like a number or boolean is typed that way; codes with leading zeros should be changed to
"type": "string". - An element that appears once in the sample is typed as an object; if it can repeat, it should be an array. Include two occurrences to get this right.
additionalProperties: falserejects unexpected keys; relax it if your XML may gain new elements.
To see the JSON the schema describes, convert your document with XML to JSON, and check the XML first with the XML Validator. For JSON input, use JSON to JSON Schema. All processing happens in your browser.
Validating converted data with the schema
Once your integration converts incoming XML to JSON, the schema can check each message before it is stored or forwarded. Two common setups:
- JavaScript: Ajv 8 validates draft-06 schemas after you register the meta-schema with
ajv.addMetaSchema(require("ajv/dist/refs/json-schema-draft-06.json")). - Python:
jsonschema.Draft6Validator(schema).validate(data)raises an error that names the failing property.
Make sure the XML to JSON step uses the same mapping rules as this generator, with attributes as plain properties and text beside attributes in value. A converter that prefixes attributes with @ or stores text as #text produces different property names, and every message would fail validation.