PrettifyPrettify

XML to JSON Converter

Paste XML and get JSON back on the same page. Prettify maps attributes to @ keys, repeated elements to arrays, and element text to values, then shows the result to copy or download. The conversion runs in your browser.

JSON
The converted result shows here as you paste.
Runs in your browser. Nothing is uploaded.

How XML maps to JSON

XML and JSON model data in different ways, so every converter must make choices. Prettify applies one fixed set of rules, so the same XML always gives the same JSON. The root element becomes the only top-level key, and each child element becomes a key inside its parent.

Two markers keep attributes and text apart from child elements. An attribute name gets an @ prefix, so id becomes "@id". When an element has both attributes and text, the text goes under the key "#text".

Key features

The rules below decide the shape of the JSON, so it helps to know them before you write code against the output. Each rule applies in the same way to every document.

  • An attribute becomes an @name key, so it never collides with a child element of the same name.
  • Elements that repeat under one parent become an array, in document order.
  • An element that appears once stays a single value, not a one-item array.
  • An empty element, such as <note/>, becomes null.
  • Every value is a string, so 84.50 stays "84.50" with its trailing zero.
  • Namespace prefixes stay in the key names, and an xmlns declaration becomes an @xmlns key.
  • A CDATA section is read as plain text.
  • A warning tells you that attributes were converted, so the shape is never a surprise.

How to convert XML to JSON

Convert in the box on this page, then continue in the editor if you need more. The box reads documents up to 512 KB. If the XML has an error, the panel shows the line and column, and the button reads "Fix it in the editor".

  1. Paste your XML into the box labeled "Paste your XML", or select "Try an example".
  2. Read the JSON in the result panel. The warning under the summary explains the attribute rule.
  3. Select Copy to put the JSON on the clipboard, or select Download to save it as prettify.json.
  4. Select "Open in full editor". The editor opens at /app/editor with the JSON in the Raw view.
  5. Switch to the Tree or Table view in the editor to check the structure.
  6. For a larger file, open the XML in the editor, select Convert in the Transform group of the toolbar, and pick JSON.

Worked example: an order

The XML below has attributes on two elements, a repeated item element, and an empty note. The block after it is the JSON that Prettify produces.

The order element has two attributes, so the JSON has @id and @status. The customer has no attributes, so it is a plain string. The item element repeats, so it becomes an array of two objects. Each object has an @sku key and its text under "#text". The total element has a currency attribute, so its text 84.50 moves under "#text" and stays a string. The empty note becomes null.

Because every value is text, a program that needs 84.50 as a number must convert it. XML has no number type, and Prettify does not guess one.

<order id="A-1001" status="paid">
  <customer>Dana Reyes</customer>
  <item sku="K-12">Keyboard</item>
  <item sku="M-07">Mouse</item>
  <total currency="USD">84.50</total>
  <note/>
</order>

{
  "order": {
    "@id": "A-1001",
    "@status": "paid",
    "customer": "Dana Reyes",
    "item": [
      {
        "@sku": "K-12",
        "#text": "Keyboard"
      },
      {
        "@sku": "M-07",
        "#text": "Mouse"
      }
    ],
    "total": {
      "@currency": "USD",
      "#text": "84.50"
    },
    "note": null
  }
}

Common uses

XML still carries a lot of data, and most modern code is easier to write against JSON. Converting is often the first step of a migration or an integration.

  • Turn an XML API response into JSON for a script or a front end.
  • Move an XML data export into a system that stores JSON.
  • Read the contents of an RSS or Atom feed as JSON. Converting a feed is free.
  • See the shape of a document before you write a parser for it.
  • Check a sitemap or a configuration file with the JSON Tree view.

What the JSON does not keep

JSON has no place for some XML details. Comments, processing instructions, and the XML declaration do not appear in the output. Mixed content loses its order. When an element holds both text and child elements, the pieces of text are joined under "#text". Their place between the children is not kept.

Order among different child names is also not kept. All children with the same name group together under one key. If the exact order of a mixed document matters, keep the XML as your source of truth.

Frequently asked questions

Is the XML to JSON converter free?

Yes. Converting XML to JSON, then viewing and copying the result, is free and needs no account. The Pro tools, such as XPath and jq, are not needed to convert.

Is my XML uploaded to convert it?

No. The parser and the converter run on your device, so a configuration file or an order export never goes to a server. Only a share link, which you create on purpose, sends data.

Why are my numbers strings in the JSON?

XML stores all values as text and has no number or boolean type. Prettify keeps the text exactly as written, which also keeps leading zeros and trailing zeros. Convert the fields you need in your own code.

Can I remove the @ prefix from the keys?

Prettify has no setting for this, because the prefix is how it tells attributes from child elements. Use Search and Replace in the editor to rewrite the keys by hand, or by regular expression.

Can I convert the JSON back to XML?

Yes. The JSON to XML page reverses the mapping: @ keys become attributes and "#text" becomes element text. One caveat: an array with a single item comes back as a single element, not an array.

Related tools