Learn how to convert XML to JSON online with examples. Follow clear steps, handle nested XML and attributes, and fix common errors.
This guide walks through a small product record, the conversion process, and common errors. For the actual conversion, use ToleHub’s free XML to JSON Converter, which supports pasted XML, file upload, and copying or downloading formatted JSON.
Before you convert: XML and JSON represent data differently
XML uses a hierarchy of elements. A tag can contain text, child elements, and attributes. JSON uses objects, arrays, and values such as strings, numbers, booleans, and null. Both can describe nested information, but there isn’t a single universal mapping from every XML document into JSON.
- XML elements: Tags such as
<name>hold content or contain other tags. - XML attributes: Data like
sku="N-100"belongs to an element’s opening tag. - JSON properties: Keys such as
"name"identify values in an object. - JSON arrays: Arrays can represent repeated sibling elements such as multiple products.
An XML converter must decide how to handle attributes, text next to child elements, repeated tags, and empty elements. Always review the generated JSON before using it in another system.
Example: convert a product XML record to JSON
Imagine exporting one product record from a supplier system. The XML might look like this:
<product sku="N-100">
<name>Notebook</name>
<stock>12</stock>
<category>Office</category>
</product>
The result will be a JSON representation of the product with fields for the name, stock, category, and SKU attribute. The exact property names and nesting depend on the converter’s mapping rules. For example, some converters use a special property for attributes, while others prefix attribute keys.
Also note that 12 in XML is text. Depending on the converter, the corresponding JSON value may be the string "12" rather than the number 12. If your application needs numeric stock counts, explicitly check or convert their types.
How to convert XML to JSON online — step by step
- Step 1: Open the XML to JSON Converter on ToleHub.
- Step 2: Paste your XML into the input area, or select a supported XML file to upload.
- Step 3: Run the conversion and examine the formatted JSON output.
- Step 4: Confirm that important attributes, repeated elements, and nested data are represented as expected.
- Step 5: Copy the JSON or download the result for your next step.
Try the short product example first, then test with a copy of your real file. That makes it easier to tell whether a problem comes from the XML structure or from the expectations of the application receiving the JSON.
What happens with nested XML and repeated elements?
XML can contain several levels of elements. For example, an order may contain a customer, an address, and a list of items. The converted JSON should retain those relationships, but the final object structure depends on how the converter groups element names.
<order id="A-18">
<customer>
<name>Sam</name>
</customer>
<item><name>Notebook</name></item>
<item><name>Pen</name></item>
</order>
The two <item> entries are repeated siblings. In a common mapping they become an array of item objects. Check whether one item and multiple items produce the same kind of JSON structure before writing code that processes the result. Your app might expect an array even when only one item exists.
How XML attributes appear in JSON
An attribute, such as id="A-18" in the example, isn’t the same thing as a child element like <customer>. Converters often distinguish the two with a naming convention or separate object. If another application expects a key called id, check the generated structure rather than assuming it appears under that exact name.
Common XML to JSON errors and fixes
1. The XML has missing or mismatched closing tags
XML requires properly nested elements. A closing tag must match the opening tag, and one element cannot be closed outside another. Fix malformed tags before trying again.
2. An ampersand or other special character breaks parsing
An unescaped ampersand in text can cause an XML parsing error. In XML content, use a valid entity such as & to represent a literal ampersand. Quotes in attribute values also need to follow XML syntax.
3. Repeated fields look different from single fields
When an XML document contains one <item>, a converter may produce an object; when it contains several, it may produce an array. Check both cases if your integration has to handle changing record counts.
4. Numbers and booleans have become strings
XML element content usually starts as text. Validate JSON types before calculations or filters. A string containing "false" is not the same as the JSON boolean false.
5. The output is valid JSON but doesn’t match the receiving API
Valid JSON alone isn’t enough. Many APIs require specific property names, array formats, and data types. Compare the result with the destination system’s schema and transform values where needed.
Can you convert XML to CSV afterward?
Yes, for data that fits a table. First use the XML to JSON converter. If your JSON can be represented as rows and columns, copy the result into ToleHub’s JSON to CSV Converter to export a CSV file. Nested data may need flattening or restructuring before it is useful in a spreadsheet.
If you’re opening the CSV in a spreadsheet, see our separate JSON to CSV for Excel walkthrough for importing and checking the file.
Frequently asked questions
Is converting XML to JSON free?
Yes. ToleHub offers a free online converter for XML data. You can paste XML or use file upload, then copy or download the result.
Does JSON preserve everything from XML exactly?
Not necessarily. Element order, mixed text, namespaces, comments, and attributes can be represented differently or require special handling. For data migration or production integrations, compare a sample of the converted output against the source.
Is XML to JSON conversion reversible?
You can convert many simple structures in both directions, but the result may not reconstruct the original XML exactly. Formatting, attributes, and mixed-content rules can make a lossless round trip difficult.
Do I need to install software?
Not to use the online ToleHub converter. If you handle large volumes or sensitive production data, a local conversion workflow may offer more control over processing, validation, and access.
Convert your XML file
Ready to test a file? Open the free XML to JSON Converter, review the output carefully, and download the JSON when it matches your needs. You can also explore all ToleHub online tools for related data tasks.
