How XML maps to JSON
- An element with only text becomes a string:
"cbc:ID": "INV-2026-0042". An empty element becomesnull. - Attributes become keys starting with
@(or_or-, as you choose), and the element's text next to them#text. - Child elements become keys by name, and a name that occurs more than once an array.
- With prefixes kept, namespace declarations stay as
@xmlnskeys, so converting back gives the same names. - Comments and processing instructions are left out; text between child elements (mixed content) is joined into one
#text.
Arrays that keep their shape
The usual trap: a list with one item becomes an object and with two an array, so code that reads the JSON breaks on the first invoice with a single line. Arrays offers three ways out:
- When an element repeats
- Arrays only where the document repeats a name. Compact, but the shape follows the data.
- For every element
- Every child is an array, always the same shape, but verbose.
- Where the XSD allows
- Open the schema, with the files it includes or imports: an element with
maxOccursabove 1, or inside a repeating sequence or choice, is an array even when it occurs once.
Strings, numbers and booleans
Every value stays a string by default, as in the XML: "00420" keeps its zeros and "1250.00" its decimals. Tick Numbers and booleans as JSON types to write 42, 1250.5, true and false as JSON values; values with leading zeros stay strings.
Questions
Is my XML uploaded?
No. The conversion runs in your browser.
Can I turn the JSON back into XML?
Yes, with the JSON to XML converter: it follows the same conventions, so the elements, attributes and text come back as they were.
What happens to namespaces?
With prefixes, keys are the names as written (cbc:ID) and the xmlns declarations are kept as @xmlns keys. With local names, prefixes and declarations are dropped: easier to read, but the XML can no longer be rebuilt the same.
How large can the document be?
Documents of a few tens of megabytes convert in your browser; the editor that shows the result gets slow past that.
Other tools
- XML formatter: Pretty-print or minify XML, keeping text, CDATA and comments as written.
- XPath tester: Evaluate XPath 3.1 against a document and see each match with its line.
- XSD generator: Infer an XML Schema from sample documents, checked against them.
- XML from XSD: Generate a sample XML document from a schema, validated against it.
- XSLT transformer: Run XSLT 1.0, 2.0 or 3.0 on a document and see the result, the messages and the files it writes.
- XML diff: Compare two XML documents as trees: changed, added and removed elements, attributes and text.
- JSON to XML converter: Convert JSON to XML: @keys become attributes, arrays repeated elements.
- XML to CSV converter: Turn a repeating element into CSV rows: invoice lines, products, records.
- E-invoice viewer: Read an XRechnung, ZUGFeRD, Factur-X or Peppol invoice as a document, and print it.
- XML validator: check a document against XSD, DTD, RelaxNG or Schematron, up to 1 GB.