Format Code Now
/
ConvertXML to JSON Schema Next: XML to Objective-C ›

XML to JSON Schema Generator

CV-83

Paste an XML document and press Convert to get a draft 2020-12 schema with types and required keys. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

XML

JSON Schema

How to use it

  1. Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
  2. Choose the indent, whether keys in every object are required, additionalProperties: false, and format detection. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
  3. Press Convert, then copy the result or download it as a .json file.
  4. Validate documents with any validator that supports draft 2020-12, and tighten the schema by hand where the sample was too loose. Feed it the JSON form of your XML, not the XML itself.

How the schema is built

The schema declares the draft 2020-12 dialect in $schema and gives every value a type: object, array, string, integer, number, boolean or null. Integers and decimals stay apart, so 1 is integer and 1.5 is number; mixed types become a type list or an anyOf. Arrays get an items schema merged from every item, and an empty array gets none. Format detection can add date-time, date, time, email, uuid, uri or ipv4 to strings, and additionalProperties: false can be switched on.

How the XML becomes a schema

An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. An element that repeats under the same parent becomes a list, while an element that appears once stays a single value. Text that is exactly a number or true/false is typed as such when the option is on (the default); a leading zero or plus sign keeps it as a string. Comments, processing instructions and the DOCTYPE are dropped, and namespace prefixes stay in the name. In the schema, @id and #text are ordinary property names, so a validator checks the converted JSON, not the XML itself.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. It writes no minimum, maximum, pattern, enum or length limits; add the rules that matter by hand. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.

Questions people ask

How do I convert XML to a JSON Schema?

Paste the XML into the input box, choose your options and press Convert to get the schema keywords. Validate documents with any validator that supports draft 2020-12, and tighten the schema by hand where the sample was too loose. The schema keywords follow the JSON form of the XML. The conversion happens in your browser.

Why are some keys not listed under required in the generated schema?

Only keys that appear in every object of the sample are required. A key missing from some objects stays optional. Turn the required option off to list none, or edit the list by hand if the sample did not show your real rules. This applies to the JSON form of your XML.

What does the additionalProperties option do in the schema?

Switched on, every object gets additionalProperties: false, so a document with a key the sample did not show fails validation. Leave it off for the default JSON Schema behavior, which accepts extra keys. This applies to the JSON form of your XML.

Does the generator write limits like minLength, enum or pattern?

No. A sample cannot show the allowed ranges, so the schema has types, properties, required, items and optional formats only. Add the limits you need by hand or in your schema editor. This applies to the JSON form of your XML.

What happens to XML attributes in the generated code?

Each attribute becomes a key that starts with @, such as @id, so it is a member of the generated type next to the child elements. The member name drops the @, and the original key is kept in the code where the language allows it. The JSON Schema schema keywords are written from that converted data.

Why is an element a list in one XML sample and a single value in another?

XML does not say how many times an element may repeat. An element becomes a list only when it appears more than once under the same parent in your sample. To get a list type, paste a sample where the element appears at least twice. The JSON Schema schema keywords are written from that converted data.

About this tool

The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. It writes a JSON Schema using the draft 2020-12 dialect: a type for every value, properties for objects, items for arrays, and null, integer and number kept apart. Keys present in every object of the sample go in required (switch that off to list none), and mixed types become a type list or anyOf. The format detection option adds date-time, date, time, email, uuid, uri or ipv4 to strings that match. It cannot know ranges, patterns, enums or lengths, so none are written. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.