On-device AI — professional-grade, fully private.

Documents

How to Convert JSON to XML (Arrays, Attributes, Root Element and Nulls Explained)

Convert JSON to XML online: paste or drop the file, name the root and array items, choose how nulls and bad key names are handled, then download valid XML.

Updated November 8, 20268 min read

To convert JSON to XML, paste the JSON or drop the file into the JSON to XML converter, set the root element and how array items are named, press Convert to XML, and download or copy the result. The button is the easy part. JSON and XML do not describe data the same way: XML needs exactly one root element, has no array type, has attributes that JSON lacks, and refuses many key names that JSON accepts without complaint. Every JSON to XML converter online has to make a decision at each of those points. This guide shows what each decision is, what the default produces, and which setting to change when the XML has to match a system that expects a particular shape.

JSON to XML in four steps

1

Add your JSON

Open the JSON to XML converter and paste JSON into the box, or drop a .json, .jsonl, .ndjson, .geojson or .txt file. A file holding one JSON value per line (JSON Lines) is recognised on its own; you do not switch a mode.

2

Name the root and the array items

Type the Root element (root by default). Under Array item naming, choose Fixed tag (every item uses the Array item tag, item by default), Parent key, or Singular.

3

Decide how values are written

Pick what Null values become, what happens to Keys that are not XML names, and whether to Map @keys to attributes. Then choose the indentation, the XML declaration and the encoding.

4

Convert and export

Press Convert to XML. The XML preview follows your settings as you change them. Download the .xml file or copy it. If you change a setting after converting, the result is marked out of date until you re-apply.

How each JSON value maps to XML

JSON has objects, arrays, strings, numbers, booleans and null. XML has elements, attributes and text. With the default settings the mapping looks like this:

JSON inXML out (defaults)Why
{"name": "Ada"}<root><name>Ada</name></root>Each key becomes a child element; the root wraps the document
"tags": ["a", "b"]<tags><item>a</item><item>b</item></tags>An array becomes the same element repeated, once per item
"age": 36<age>36</age>XML has no number type; the digits are written as text
"active": true<active>true</active>Booleans are text too
"city": null<city></city>Null becomes an empty element unless you choose otherwise
"meta": {} or "list": []an empty elementNothing inside, so nothing is written inside

Keys keep the order they have in your source, including keys that look like numbers. Numbers are copied character for character, so a 20-digit order id stays exact and 1e-7 stays 1e-7 instead of being rounded. Characters such as &, < and > are escaped. Control characters that XML cannot hold at all are removed, and the tool tells you how many.

The root element: XML needs exactly one

A JSON file can start with an object, an array, or even a single string. An XML document must have exactly one top-level element, so the converter has to invent one. That is the Root element box. The default is root; change it to whatever the receiving system expects, such as orders, response or catalog.

If you type a name that is not legal in XML, for example my data with a space, it is repaired and the change is shown rather than applied silently. When the top of your JSON is an array, every item is written under the root, and the root name also stands in as the parent key for the array naming modes below.

Arrays: fixed tag, parent key or singular

XML has no array. A list is written as the same element repeated, and the converter has to decide what that element is called. Take {"books": [{"title": "Emma"}, {"title": "Dune"}]}:

Array item namingOutputUse it when
Fixed tag (default)<books><item>…</item><item>…</item></books>A consumer expects one generic tag for every list
Parent key<books><books>…</books><books>…</books></books>The schema repeats the container name
Singular<books><book>…</book><book>…</book></books>You want the XML a person would write by hand

Singular handles ordinary English plurals: books gives book, categories gives category, children gives child. When a word cannot be shortened, the fixed tag is used instead, so you never get an element nested inside one with the identical name by accident. A JSON Lines file is treated like a top-level array: each line becomes one repeated element under the root.

Attributes with @keys and #text

JSON has nothing that corresponds to an XML attribute, so attributes need a convention. Turn on Map @keys to attributes and a key beginning with @ becomes an attribute of its element, while the key #text becomes the element's own text. So {"price": {"@currency": "EUR", "#text": "9.50"}} is written as <price currency="EUR">9.50</price>.

  • It is off by default, because @ and #text may simply be ordinary keys in your data.
  • An attribute can only hold flat text. If an @key holds an object or an array, it is written as a normal child element instead, and the count is reported. Nothing is dropped.
  • It is the same convention the XML to JSON tool reads, so data converted one way and back keeps its shape. The details of that direction are in the guide to converting XML to JSON.

Keys that are not valid XML names

Any string can be a JSON key. An XML element name must start with a letter or an underscore, cannot contain spaces or most punctuation, and names beginning with the letters xml are reserved. Exports from spreadsheets and APIs break these rules constantly: first name, 2024, unit-price ($). The setting Keys that are not XML names gives two choices.

Setting"first name": "Ada" becomesTrade-off
Repair (default)<first_name>Ada</first_name>Clean names; every rename is listed beside the preview
Keep in an attribute<key name="first name">Ada</key>The original text survives exactly; the XML is harder to query

A leading digit gets an underscore in front rather than being deleted, so 2024 becomes _2024. If two keys repair to the same name, such as first name and first-name, the second becomes first_name_2, so neither value is lost. Accented and non-Latin keys like Führung or 名前 are already legal XML names and are kept as they are. Emoji in keys become an underscore, because XML parsers disagree about accepting them.

Nulls, declaration, indentation and encoding

A JSON null has three possible spellings in XML, and they mean different things to whoever reads the file:

  • Empty element, the default: <city></city>. Most consumers read this as an empty string.
  • xsi:nil: <city xsi:nil="true"/>, with the schema-instance namespace declared on the root. This is the form an XSD-validated system expects for "known to have no value".
  • Omit: the element is not written. This applies to object members only. A null inside an array is still written, because removing it would shift the position of every item after it.

Under Output, the XML declaration line states the version and encoding at the top of the file and is on by default. Indentation can be 2 spaces, 4 spaces, Tab, or None, which writes the whole document on one line for programs rather than people. Indenting never adds whitespace inside an element that holds text, so the pretty and the compact output carry the same values. Encoding offers UTF-8, UTF-16 and ISO-8859-1, and the bytes really are encoded that way. In ISO-8859-1, characters outside Latin-1 are written as numeric character references such as &#8364; for the euro sign, which is legal XML and loses nothing. Pick UTF-8 unless the receiving system says otherwise.

When the conversion fails, and what Free covers

Invalid JSON is the most common failure. The converter shows the message, the line and column, and a snippet with the position marked; a trailing comma, a single-quoted string or a missing bracket is usually the cause. The JSON Validator is the quickest place to fix it. Duplicate keys are legal JSON but ambiguous, so they are reported instead of quietly keeping the last one.

FreePro
Files per run1Up to 50, downloaded as one ZIP
Size per file10 MBNo set cap, up to what your device can handle
Root, array naming, nulls, name repair, attributesYesYes
Indentation, declaration, all three encodingsYesYes
XSD schema, namespace and prefix, type annotationsNoYes

A file over 10 MB is refused up front with its actual size. Whitespace is often a large share of a pretty-printed export, so running it through the JSON Minifier first can bring it under the cap. Expect the XML to be larger than the JSON, roughly double for typical data, because every value is wrapped in an opening and a closing tag. If your data starts in a spreadsheet, CSV to XML skips the JSON step entirely.

Paste or drop JSON, name the root and array items, choose how nulls and bad key names are handled, and download valid XML. Free: one file up to 10 MB with every structure and output setting. Pro: up to 50 files as a ZIP, XSD, namespaces and type annotations. It all runs on your device, not our servers.

Convert JSON to XML

Frequently asked questions

How do I convert JSON to XML online?

Paste the JSON or drop the file into the JSON to XML converter, set the root element and how array items are named, press Convert to XML, then download or copy the XML. Free covers one file up to 10 MB.

How is a JSON array converted to XML?

As the same element repeated once per item, inside an element named after the array's key. The repeated element is called item by default; you can name it after the parent key or its singular, so a books array writes book elements.

Can I create XML attributes from JSON?

Yes. Turn on Map @keys to attributes: a key starting with @ becomes an attribute, and #text becomes the element's text. An @key holding an object or array is written as a child element instead.

Why does my XML have first_name when the JSON said first name?

Spaces are not allowed in XML element names, so the key was repaired. Every rename is listed beside the preview. Choose Keep in an attribute to write the original text in a name attribute instead.

What happens to null values when converting JSON to XML?

By default a null becomes an empty element. You can write it as xsi:nil, which schemas understand, or omit it. Omit applies to object members only; nulls inside arrays are always written.

Why is the XML file bigger than my JSON?

Each value in XML sits between an opening and a closing tag that repeat the element name, where JSON names a key once. For typical data the XML comes out about twice the size. Indentation None makes it smaller.

Keep reading

More in Documents →