JSON Schema from XML

Generate a JSON Schema from XML.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What XML to JSON Schema does

XML to JSON Schema creates a JSON Schema (draft-07) that describes the structure of a sample XML document: the type of every value, the properties of every object and the item type of every array. It is a starting point for request validation or documentation, to be tightened by hand.

XML to JSON Schema examples

The built-in sample

Input

<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>

Output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "@id": {
        "type": "string"
      },
      "name": {
        "type": "string"
      }
    },
    "required": [
      "@id",
      "name"
    ]
  }
}

This is the input that the Load sample button fills in.

Attributes, repeated elements and empty elements

Input

<order id="7" paid="false"><total>19.5</total><item sku="A1"><qty>2</qty></item><item sku="B2"><qty>1</qty><gift/></item><note/></order>

Output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "@id": {
      "type": "string"
    },
    "@paid": {
      "type": "string"
    },
    "total": {
      "type": "string"
    },
    "item": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "@sku": {
            "type": "string"
          },
          "qty": {
            "type": "string"
          }
        },
        "required": [
          "@sku",
          "qty"
        ]
      }
    },
    "note": {
      "type": "string"
    }
  },
  "required": [
    "@id",
    "@paid",
    "total",
    "item",
    "note"
  ]
}

In the sample, id, paid and sku are attributes, item repeats, and gift and note are empty elements. Every value is typed as a string, because XML does not carry types.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively. Each object becomes {"type":"object"} with a properties entry per key, and every key seen in the sample is listed in required. Arrays use the schema of their first item for items; an empty array gets an empty items schema, which accepts anything. Whole numbers become integer, other numbers number, and null values {"type":"null"}.

Review the result: remove optional fields from required, and widen null types (for example to ["string","null"]) where a field can hold a value. Unlike the code generators, the schema generator looks only at the first item of an array, so a field that appears only in later items is missing from the schema.

XML is first read with the XML to JSON rules: attributes become properties whose names start with @, repeated elements become arrays, and every leaf value is a string, so the schema never contains integer or boolean.

Details and limits

  • Values are carried across without losing anything in ordinary data; the points above list the cases the two formats handle differently.
  • The schema declares draft-07 in $schema. It contains only type, properties, required and items: no formats, patterns, enums or length limits are guessed from the sample.
  • additionalProperties is not set, so the schema accepts objects with extra keys.
  • A top-level array gives a schema of type array; a top-level string or number gives a schema with just that type.
  • Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.

Questions

Is my data uploaded when I use XML to JSON Schema?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does XML to JSON Schema do?

XML to JSON Schema creates a JSON Schema (draft-07) that describes the structure of a sample XML document: the type of every value, the properties of every object and the item type of every array. It is a starting point for request validation or documentation, to be tightened by hand.

How does XML to JSON Schema work?

The sample is walked recursively. Each object becomes {"type":"object"} with a properties entry per key, and every key seen in the sample is listed in required.

Are there any limits I should know about?

The schema declares draft-07 in $schema. It contains only type, properties, required and items: no formats, patterns, enums or length limits are guessed from the sample. additionalProperties is not set, so the schema accepts objects with extra keys.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.