Infer JSON Schema from a Simple XML Element
Learn how a single XML element with text content maps to a JSON Schema property with automatic type inference for strings, numbers, and booleans.
Detailed Explanation
From XML Elements to JSON Schema Properties
The simplest XML-to-JSON-Schema conversion takes a single element with text content and produces a schema with a typed property. The tool examines the actual text value to determine the most specific type.
Example XML
<product>
<name>Widget</name>
<price>29.99</price>
<quantity>100</quantity>
<inStock>true</inStock>
</product>
Generated JSON Schema (Draft 7)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"product": {
"type": "object",
"properties": {
"name": { "type": "string" },
"price": { "type": "number" },
"quantity": { "type": "integer" },
"inStock": { "type": "boolean" }
}
}
}
}
Type Inference Rules
| XML Text Value | Inferred JSON Schema Type |
|---|---|
Widget |
string |
29.99 |
number (decimal) |
100 |
integer (whole number) |
true / false |
boolean |
How It Works
The converter uses the browser's built-in DOMParser to parse the XML into a DOM tree. It then walks the tree recursively. For each element that contains only text (no child elements), it analyzes the text content to determine the most appropriate JSON Schema type. Whole numbers become integer, decimal numbers become number, the literals true and false become boolean, and everything else defaults to string.
The root element becomes the top-level property in the schema, wrapping the entire structure. This mirrors the JSON convention where an XML root element corresponds to a top-level object key.
Use Case
When you receive XML data from an API or configuration file and need to create a JSON Schema for validation. This is common when migrating from XML-based APIs to JSON-based APIs and you need schema definitions for the new format.