ToolNest logoToolNest.

How to Convert XML to JSON Online Free

To convert XML to JSON online free, paste your XML into a converter, which maps each element to a JSON object, attributes to keys, and repeated elements to arrays, then copy the JSON output. The whole conversion takes seconds and needs no software install. Convert your file instantly with our free JSON formatter.

Try it now: JSON Formatter →

What XML to JSON Conversion Actually Does

XML and JSON describe the same kind of thing, nested data with names and values, but they do it with different shapes, so converting between them is a translation, not a copy. An XML element such as a book with a title child becomes a JSON object with a title key, and the text inside the element becomes the value for that key. An XML attribute, which sits inside the opening tag, has no direct JSON equivalent, so converters usually turn it into an ordinary key, often with a marker such as an at sign in front of it, so that an element with an id attribute and a title child becomes an object with two keys, one for the id and one for the title. Repeated elements are the other big decision: a list of three book elements cannot become three separate keys with the same name, because JSON objects cannot repeat a key, so the converter groups them into an array, a list under one key. The root element wraps everything, so a catalog element containing books becomes an object with a single catalog key holding the rest of the structure. None of this changes the meaning of the data, but every converter makes the same handful of choices slightly differently, which is why the output of one converter can look different from another while still describing the same information. Understanding those choices matters because the JSON you get is only useful if the program reading it expects the same choices, and a mismatch there is the most common reason a converted file fails downstream.

Try it now: JSON Formatter →

Step by Step: Convert XML to JSON Online Free

Converting XML to JSON online takes five short steps, and doing them in order keeps the result clean. Step one, copy the XML from wherever it lives, a file, an API response, or a configuration export, and paste it into the converter, checking that you copied from the first angle bracket to the last, because a truncated paste is the most common cause of a failed conversion. Step two, confirm the XML is well formed, meaning every opening tag has a matching closing tag and the nesting never crosses, since a converter cannot map a structure that does not close properly. Step three, run the conversion and read the JSON that comes back, checking the top level first: the root element should appear as a single key, and the main repeated elements should appear as arrays, not as a single object that silently dropped all but the last item. Step four, validate the JSON with a formatter, which catches trailing commas, missing brackets, and other syntax slips that break parsers, and our free JSON formatter does that check and pretty-prints the result in one pass. Step five, copy the JSON into its destination, whether that is a JavaScript file, a test fixture, or an API request body, and run the receiving program once against it before treating the conversion as done. If the receiving program complains, the problem is almost always one of the mapping choices in the first section, attributes or repeated elements, rather than the data itself.

The Three Decisions Every Converter Makes

Every XML to JSON converter, online or in code, answers three questions, and knowing its answers saves hours of debugging. The first question is attributes: does the converter put attribute values under keys marked with an at sign, such as @id, or under plain keys, such as id, and does it keep the attribute separate from the element text or merge them. The second question is text content: when an element holds both text and child elements, does the converter store the text under a special key such as #text, or does it drop the text, or does it make the whole element a string and lose the children. The third question is repeated elements: a single book element might convert to one object, while two book elements convert to an array, which means the shape of the output changes with the number of items, and code that reads the JSON must handle both shapes or it breaks on the first single-item file. Some converters solve the third problem by always using arrays for known list elements, and some solve it by never using arrays, instead numbering the keys, but the common default is the shape-changing behavior, and it deserves a test with both one item and several items before the conversion is trusted. Namespaces are a fourth wrinkle worth one sentence: an XML tag with a prefix such as soap:Envelope keeps that prefix in most converters, so the JSON key reads soap:Envelope, and stripping prefixes is a separate cleanup step, not something the conversion does by itself.

Common XML Shapes and How They Convert

Most XML people convert falls into a few shapes, and each one converts cleanly once its pattern is recognized. A configuration file, with named settings and values, becomes a flat or shallow object, one key per setting, and it is the easiest shape to convert, because there are no repeated elements to trip the array rule. A product catalog, with repeated product elements each holding a name, a price, and a stock flag, becomes an object holding an array of product objects, and that array is the shape a storefront script or a spreadsheet import expects. A feed, such as an RSS feed with repeated item elements holding titles, links, and dates, converts the same way, one array of items under the channel key, and the dates arrive as strings, not as date values, so any date sorting has to parse them afterwards. A deeply nested document, such as an invoice with a customer block, an address block, and a list of line items, becomes nested objects with the line items as an array, and the nesting depth is preserved exactly, which is good, because flattening it would lose which address belongs to which party. The worked pattern to remember is simple: one element becomes an object, repeated elements become an array, attributes become keys, and text becomes a value, and any XML file, however large, is just those four rules applied again and again. When a file looks confusing, find its repeated elements first, because they decide the array structure, and the rest of the file falls into place around them.

How to Convert XML to JSON in Python

When the conversion has to run repeatedly, code beats a browser converter, and Python offers two honest routes. The quick route is the xmltodict library, which parses XML into Python dictionaries that dump straight to JSON with the standard json module, and its defaults match the common converter choices: attributes get an at-sign prefix, text content gets a #text key, and repeated elements become lists. The built-in route is ElementTree from the standard library, which parses the XML into a tree that you walk yourself, turning each element into a dictionary, which is more work but gives full control over the attribute and repeated-element decisions, and it needs no third-party install. Either way, the steps are the same: parse, walk the tree, build dictionaries, then dump with indentation for readability, and the dump step is where a formatter helps, because an unindented single-line JSON string is valid but unreadable, and readability is what lets you spot a mapping mistake. For a one-off file, the online converter is faster than writing a script, and for a pipeline that converts nightly exports, the script is faster than pasting, and the dividing line is simply how often the conversion repeats. Our guide to converting CSV to JSON online covers the same idea for spreadsheet data, our guide to converting JSON to CSV covers the reverse trip for tabular data, and our guide to what JSON is explains the format the conversion produces, including why JSON has no attributes and no comments.

Mistakes, Validation, and Going Back

Five mistakes account for most failed XML to JSON conversions, and each one is easy to spot once named. First, pasting truncated XML, which fails with a mismatched-tag error and is fixed by re-copying the whole file. Second, assuming the output shape is stable, then breaking on a single-item file that produced an object where a multi-item file produced an array, fixed by testing both shapes. Third, losing attributes, because a converter that merges attributes and text silently drops one of them, fixed by checking one element with an attribute before converting a large file. Fourth, character encoding trouble, where a file saved in a legacy encoding arrives with garbled accented characters, fixed by saving as UTF-8 before converting. Fifth, converting a huge file in a browser tab and watching the tab stall, which is a sign to switch to a script, since browsers handle small and medium files well but not multi-megabyte ones. After conversion, validate the JSON, keep the original XML until the receiving program has accepted the JSON, and remember the reverse trip exists: converting JSON back to XML needs the same mapping choices in reverse, and a round trip rarely reproduces the original XML byte for byte, because attribute markers and whitespace do not survive. If the converted file is part of a document you are timing, our free minutes to hours converter turns the minutes spent on a batch into decimal hours for a log, and our free word counter keeps any write-up of the conversion inside a word limit.

Do it in one click

Format and validate JSON instantly — free, no signup.

Open the Free Tool →