Conversion basics
How to Convert XML to JSON
To convert XML to JSON, parse the XML document into a structured object, then serialize that object as JSON. With the online converter, paste your XML into the input or choose a local XML file and review the formatted result in your browser.
- Start with well-formed XML that has one document root.
- Run the conversion, then inspect nested elements and text values.
- Check how attributes and repeated sibling elements were represented.
- Copy or download the JSON after confirming it matches your application’s expected shape.
XML and JSON do not have identical data models. XML attributes may need their own JSON keys, and repeated XML elements are commonly represented as arrays. A single repeated element may be handled differently from several repeated elements depending on the parser’s settings. Review those cases before relying on a converted document in production.
Convert XML online →Format comparison
JSON vs. XML: Which Format Should You Use?
JSON is usually a convenient choice for web APIs and applications that exchange objects, lists, strings, numbers, and boolean values. Its compact syntax is supported directly by JavaScript and is widely used across modern development tools.
XML can be the better choice when an existing system requires it or when a workflow depends on XML namespaces, attributes, mixed content, document markup, or schema-based validation. It is also common in established enterprise integrations and document-oriented formats.
Choose based on the receiving system and the data you need to represent, not just file size or familiarity. If a partner API specifies XML, use XML; if you control a straightforward web API, JSON may be simpler. Converting between them can change representation, so preserve the source whenever exact XML details matter.
API integration
How to Pass XML Data in a JSON Request
If an API accepts JSON but one field must contain XML, represent the XML document as a JSON string. Escape quotation marks inside the XML string so the complete request remains valid JSON. For example, an XML value such as <note><to>Ada</to></note> can be carried as:
{
"xml": "<note><to>Ada</to></note>"
}
Send the request with Content-Type: application/json only when the endpoint expects a JSON body. The API may instead require raw XML with application/xml. Check its contract, size limits, and escaping rules before sending data.
Developer examples
XML to JSON in Python, npm, and C#
An online XML to JSON converter is handy for one-off checks. For a recurring job, choose a parser that fits your runtime, configure how it handles attributes and repeated tags, and add tests for representative input. These short examples show common starting points; review each library’s documentation and version before using it in production.
Python and Python 3
For Python XML to JSON conversion, xmltodict parses XML into dictionaries that can be serialized with the standard json module. The same approach works in Python 3. Decide whether attributes should keep a prefix and test whether repeated elements have the array shape your application expects.
import json
import xmltodict
data = xmltodict.parse(xml_text)
json_text = json.dumps(data, indent=2)
npm and Node.js
When searching for an npm XML to JSON parser, compare maintenance, supported Node.js versions, security posture, and parser options. This site uses fast-xml-parser for browser conversion. Its parser can also be used in a Node.js project to turn XML text into a JavaScript value before calling JSON.stringify.
import { XMLParser } from 'fast-xml-parser';
const parsed = new XMLParser({
ignoreAttributes: false,
}).parse(xmlText);
const jsonText = JSON.stringify(parsed, null, 2);
C# and .NET
For C# XML to JSON conversion, a common route is to parse XML with XDocument and use Newtonsoft.Json’s XML conversion support. Install the package, validate or safely parse untrusted XML according to your application’s security requirements, and check the library’s conventions for attributes and root elements.
using System.Xml.Linq;
using Newtonsoft.Json;
var document = XDocument.Parse(xmlText);
var jsonText = JsonConvert.SerializeXNode(
document,
Formatting.Indented
);
Automation and integrations
Power Automate and WSO2 XML to JSON workflows
A Power Automate flow that needs to convert XML to JSON usually has to parse or query the incoming XML and map its values into the JSON shape expected by the next action. Workflow expression functions such as xml() and xpath() can help work with XML data, but the right expression depends on the trigger output and the XML structure. Test optional nodes, namespaces, repeated elements, and empty values in the actual flow. Do not assume a JSON parsing action alone converts arbitrary XML text.
For WSO2 XML to JSON integration, use the message transformation and format-handling capabilities supported by the WSO2 product and version in your deployment. Verify the input content type, mediation or mapping configuration, and output contract in the relevant WSO2 documentation. A browser converter helps inspect a sample payload, but production integration should account for schemas, errors, namespaces, and the behavior of downstream services.
Across Python, npm, C#, Power Automate, and WSO2, the central question is the target data model: how should attributes, repeated tags, and text be represented? Keep example XML fixtures, test the converted output against the receiving API, and preserve the original input where audit or reprocessing requirements apply.