# JSON to XML converter

Source: https://xmlvalid.com/tools/json-to-xml

> Turn JSON into well-formed XML: keys become elements, @name keys attributes, #text the element's text and arrays repeated elements. It is the reverse of the XML to JSON converter, so a document converted there comes back the same. Everything runs in your browser.

## How JSON maps to XML

- An object with one key, like `{"order": {…}}`, makes that key the document element. Anything else is wrapped in a root element you name.
- Keys become child elements; an array repeats its element once per item, and the items of a top-level array become `item` elements.
- Keys starting with `@` (or the prefix you choose) that hold a string, number or boolean become attributes; `#text` becomes the element's text.
- `null` becomes an empty element; numbers and booleans are written as JSON writes them.

## Keys that are not XML names

XML names can't contain spaces or start with a digit, so `"delivery date"` becomes `delivery_date` and `"2x"` becomes `_2x`; the renamed keys are listed under the result. `&`, `<` and `>` in values, and quotes in attributes, are escaped.

## After converting

The XML has no schema yet. Generate one from the result with the [XSD generator](https://xmlvalid.com/tools/xsd-generator), then check new documents against it in the [validator](https://xmlvalid.com/validate).

## Questions

### Is my JSON uploaded?

No. The conversion runs in your browser.

### How do I make an attribute?

Prefix the key: {"price": {"@currency": "EUR", "#text": "75.00"}} gives &lt;price currency="EUR"&gt;75.00&lt;/price&gt;.

### Can the XML have namespaces?

Yes: write the declarations as keys on the root object ("@xmlns" or "@xmlns:cbc") and use prefixed keys such as "cbc:ID".

### What if the JSON is invalid?

The converter stops at the first syntax error and shows its line and column.

### Other tools

- [XML formatter](https://xmlvalid.com/tools/xml-formatter): Pretty-print or minify XML, keeping text, CDATA and comments as written.
- [XPath tester](https://xmlvalid.com/tools/xpath-tester): Evaluate XPath 3.1 against a document and see each match with its line.
- [XSD generator](https://xmlvalid.com/tools/xsd-generator): Infer an XML Schema from sample documents, checked against them.
- [XML from XSD](https://xmlvalid.com/tools/xsd-to-xml): Generate a sample XML document from a schema, validated against it.
- [XSLT transformer](https://xmlvalid.com/tools/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](https://xmlvalid.com/tools/xml-diff): Compare two XML documents as trees: changed, added and removed elements, attributes and text.
- [XML to JSON converter](https://xmlvalid.com/tools/xml-to-json): Convert XML to JSON, with arrays where an element repeats or wherever its XSD allows.
- [XML to CSV converter](https://xmlvalid.com/tools/xml-to-csv): Turn a repeating element into CSV rows: invoice lines, products, records.
- [E-invoice viewer](https://xmlvalid.com/tools/e-invoice-viewer): Read an XRechnung, ZUGFeRD, Factur-X or Peppol invoice as a document, and print it.
- [XML validator](https://xmlvalid.com/validate): check a document against XSD, DTD, RelaxNG or Schematron, up to 1 GB.
