How to Convert an XML File to JSON on Android

You have XML but the tool, script or teammate on the other end wants JSON. On a computer you would reach for a converter; on a phone the options are thinner. This guide explains how the two formats map onto each other - which matters if you want correct JSON, not just something that looks close - and how to convert an XML file to JSON on Android.

Short version: open the XML in a viewer that can export to JSON, run the export, and save or share the result. The important part is knowing how attributes, text and repeated elements are represented.

Why XML to JSON is not a one-to-one swap

XML and JSON both describe structured data, but they do not have the same building blocks. XML has elements, attributes and text content; JSON has objects, arrays, strings, numbers and null. A few things have to be decided during conversion:

  • Attributes have no direct JSON equivalent, so they need somewhere to live.
  • Text inside an element that also has attributes needs its own key.
  • Repeated elements (several <item> in a row) should become a JSON array.
  • Empty elements need a defined value.

A good converter makes these choices consistently. In XML Viewer, attributes are stored under @attributes, text values use #text when needed, repeated elements become arrays, and empty elements become null.

What the conversion looks like

Given this XML:

<book id="1">
  <title>A</title>
  <tag>new</tag>
  <tag>sale</tag>
</book>

the exported JSON keeps the attribute, the text and the repeated element each in the right place:

{
  "book": {
    "@attributes": { "id": "1" },
    "title": "A",
    "tag": ["new", "sale"]
  }
}

How to convert an XML file to JSON on Android, step by step

1. Open the XML file

Install XML Viewer from Google Play, tap Open XML file and pick your file, or open it directly from another app.

2. Export as JSON

Use Export to JSON. XML Viewer converts the full document into formatted JSON, handling attributes, text values and repeated elements as described above. JSON export is a Pro feature.

3. Save or share the JSON

Save the converted JSON to a file or share it with another app, so you can use it wherever you need JSON instead of XML.

The app behind this guide

XML Viewer is my own Android app, and it is what the steps above describe. It opens the file in a formatted tree view, works completely offline for local files, and the core viewer is free with no account required. Your files stay on your device - no uploads, no cloud processing.

Free covers the essentials: open files, read the formatted tree, expand and collapse nodes, search text, jump to a line, copy values, and switch theme. Pro adds larger files, XPath search, saved bookmarks, Copy XPath, save and share, and JSON/PDF export.

Get it on Google Play

Keep reading


↑