How to use the xml formatter and xml to json
- Paste your XML into the box, choose an XML file, or press Use a sample.
- Choose Do this: format with indents, minify to one line, or convert to JSON. Pick the indent, and choose whether to keep comments and the XML declaration.
- Read the Result box. If the XML has a mistake, the message gives the line and column, and Show the problem in the text selects that spot for you.
- Press Copy result or Download. A formatted file is saved as formatted.xml and a conversion as converted.json.
From XML to JSON
The XML library with the attribute name set to City & Co, two book elements and a tags element holding three tag elements becomes one JSON object. Attributes get an at sign in front, so name becomes @name with the value City & Co. The two book elements become a list, and the three tag elements become the list a, b, c.
An element that holds text and attributes at once, such as note with lang set to en and the words Hello and there around a CDATA section, becomes an object with @lang and #text. An empty element becomes an empty string. The number 007 stays the text 007, because no value is turned into a number or a boolean.
What formatting changes
Formatting puts each element on its own line and indents children. Only blank space between elements is added or removed. Text inside an element is kept as written, and an element that mixes text with child elements, such as a paragraph with bold words, is left on one line exactly as it was, because moving its content would change what it says. Whitespace-only elements become empty tags.
Minifying removes blank space between elements and, optionally, comments and the declaration. Both modes write special characters correctly, so an ampersand or a less-than sign in your text comes out as an entity again.
A strict and safe reader
The reader follows the main XML rules. Tags must close in the reverse order they opened, attribute values must be in quotes, a document has exactly one root element, and an ampersand must start an entity. Mistakes are reported with their line and column rather than quietly repaired.
It never expands entities that a document defines for itself, never loads an external file and never turns text into page markup. A DTD, which is the part of a DOCTYPE written in square brackets, is refused, so documents built to blow up in size or read local files do nothing. Nesting is limited to 300 levels and the text to 5 million characters. For JSON, see the JSON formatter and validator, the JSON viewer and the CSV to JSON converter.
Assumptions and limits
- Only the five built in entities and numeric character references are known. Other entities such as need to be written as characters.
- XML namespaces are not interpreted. Names such as xs:element are kept exactly as written.
- JSON keeps element order only within groups of the same name, because JSON objects group repeated names into lists.
- Large documents are limited to 5 MB so your browser stays responsive.
Frequently asked questions
How do I pretty print XML?
Paste it, leave the result on Format with indents, and choose 2 spaces, 4 spaces or a tab. Each element goes on its own line with its children indented.
How is XML turned into JSON here?
Attributes get an @ prefix, text next to attributes or child elements is stored as #text, repeated elements become lists, and all values stay text. The page explains this next to the result.
Why does the tool refuse my DOCTYPE?
A DOCTYPE with its own declarations in square brackets can define entities that expand into huge text or read files. This tool does not allow it, so remove that part and try again.
What does an error with a line and column mean?
The reader found the first thing that breaks the rules at that position, such as a tag that closes in the wrong order. Fix that spot and the next problem, if any, is reported.