How to convert SOAP XML to JSON
SOAP services still power banking, payments, logistics and government APIs, but most modern code wants JSON. Here is how to convert a SOAP response to JSON online in one click, what the result looks like, and how to strip the envelope and namespace prefixes in JavaScript or Python so you get clean, usable data.
Why SOAP XML is awkward to convert
A SOAP response is ordinary XML, but the data you actually want is wrapped in several layers: an Envelope, an optional Header, a Body, and usually a …Response element named after the operation. Every element also carries a namespace prefix such as soap: or m:. A straight conversion keeps all of that, so the JSON is correct but clumsy to use.
This guide uses one small order response throughout:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:m="http://example.com/orders">
<soap:Header/>
<soap:Body>
<m:GetOrderResponse>
<m:Order id="1042" status="shipped">
<m:Customer>Asha Rao</m:Customer>
<m:Total currency="INR">2499.00</m:Total>
<m:Item>Keyboard</m:Item>
<m:Item>Mouse</m:Item>
</m:Order>
</m:GetOrderResponse>
</soap:Body>
</soap:Envelope>
Convert SOAP XML to JSON online
- Open the Tagbrace formatter and paste the whole SOAP response, including the
<?xml …?>line and the envelope. - Leave Infer types switched on if you want numbers and booleans in attributes and text to become JSON numbers and booleans.
- Click XML → JSON. The conversion runs in your browser, so the message — which may contain customer or account data — is never uploaded.
For the example above, Tagbrace produces:
{
"soap:Envelope": {
"@xmlns:m": "http://example.com/orders",
"@xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"soap:Header": null,
"soap:Body": {
"m:GetOrderResponse": {
"m:Order": {
"@id": 1042,
"@status": "shipped",
"m:Customer": "Asha Rao",
"m:Total": {
"@currency": "INR",
"#text": "2499.00"
},
"m:Item": [
"Keyboard",
"Mouse"
]
}
}
}
}
}
Four things to notice:
- Prefixes stay in the keys —
"soap:Body","m:Order". They are part of the element names in the XML. - Attributes become
@keys —id="1042"becomes"@id": 1042, and namespace declarations appear as"@xmlns:…". - Text next to attributes goes in
#text—<m:Total currency="INR">2499.00</m:Total>becomes an object with"@currency"and"#text". The amount stays the string"2499.00"so the trailing zeros are not lost. - Repeated elements become an array — the two
<m:Item>elements become["Keyboard", "Mouse"]. An empty element such as<soap:Header/>becomesnull.
How each SOAP part maps to JSON
| In the SOAP XML | In the JSON |
|---|---|
<soap:Envelope>, <soap:Body> | Outer objects you normally skip over |
xmlns:m="…" | "@xmlns:m": "…" — safe to delete once prefixes are gone |
<m:Order id="1042"> | "m:Order": { "@id": 1042, … } |
<m:Customer>Asha Rao</m:Customer> | "m:Customer": "Asha Rao" |
| Element with attributes and text | Object with @… keys and "#text" |
| Same element twice or more | An array |
<soap:Header/> (empty) | null |
Unwrap the envelope and drop prefixes
Most applications only need what is inside Body, without prefixes. After converting, the result you usually want looks like this:
{
"id": 1042,
"status": "shipped",
"customer": "Asha Rao",
"total": { "amount": "2499.00", "currency": "INR" },
"items": ["Keyboard", "Mouse"]
}
To get there by hand: copy the object under "m:Order", remove the m: from each key, delete the "@xmlns…" keys, and rename keys to the style your API uses. For anything you do more than once, do it in code as shown below — the library options handle prefixes and arrays for you.
Watch out: one item vs many items
XML has no separate syntax for lists, so a converter can only guess. If a response has two <m:Item> elements you get an array; if it has one, you get a plain value:
"m:Item": "Keyboard"
Two <m:Item> → an array"m:Item": ["Keyboard", "Mouse"]
Code that loops over Item then works for some orders and crashes on others. The fix is to tell your parser which elements are always lists — isArray in fast-xml-parser, force_list in xmltodict — or to wrap single values in an array yourself before using them.
Convert SOAP XML to JSON in JavaScript (Node.js)
fast-xml-parser can strip prefixes and keep attributes in the same @ / #text style that Tagbrace uses:
import { XMLParser } from "fast-xml-parser";
const parser = new XMLParser({
ignoreAttributes: false, // keep id="1042", currency="INR"
attributeNamePrefix: "@",
textNodeName: "#text",
removeNSPrefix: true, // soap:Body -> Body, m:Order -> Order
parseTagValue: false, // keep "2499.00" as a string
isArray: (name) => name === "Item", // always an array, even with one item
});
const data = parser.parse(soapXml);
const order = data.Envelope.Body.GetOrderResponse.Order;
console.log(JSON.stringify(order, null, 2));
This prints the order with plain keys (Customer, Total, Item), "@id": "1042", and the total as "2499.00". Without parseTagValue: false the total would become the number 2499 and the trailing zeros would be lost — avoid that for money.
Convert SOAP XML to JSON in Python
xmltodict also uses @ for attributes and #text for text. Mapping each namespace URI to None removes the prefixes:
import json
import xmltodict
ns = {
"http://schemas.xmlsoap.org/soap/envelope/": None, # drop the soap: prefix
"http://example.com/orders": None, # drop the m: prefix
}
data = xmltodict.parse(
soap_xml,
process_namespaces=True,
namespaces=ns,
force_list=("Item",), # always a list, even with one item
)
order = data["Envelope"]["Body"]["GetOrderResponse"]["Order"]
order.pop("@xmlns", None) # remove leftover namespace declarations
print(json.dumps(order, indent=2))
Values come back as strings ("@id": "1042", "#text": "2499.00"), so convert the fields you need to numbers explicitly. Map the namespace URI, not the prefix — another server may use ns1: instead of m: for the same namespace.
Handling SOAP faults
When a SOAP call fails, the Body contains a Fault instead of the normal response. Check for it before reading your data, or you will get a confusing “undefined” or KeyError instead of the real error:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Order 1042 not found</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
In SOAP 1.1 the fault has faultcode and faultstring. SOAP 1.2 uses the namespace http://www.w3.org/2003/05/soap-envelope and nests the details in Code/Value and Reason/Text. After conversion, test for a Fault key under Body and surface its message.
If the conversion fails
- “prefix is unbound” or “undeclared prefix” — you copied only part of the message, such as the
Body, and left thexmlns:declarations behind on the envelope. See how to fix an undeclared namespace prefix. - Content before the declaration — HTTP headers or log text pasted above
<?xml. Delete everything before the first<. - MTOM / multipart responses — if the response starts with
--uuid:boundary lines, it is a multipart message. Extract the XML part between the boundaries first.
For other parser errors, see how to fix common XML errors.
Related guides
- XML namespace prefix not declared: how to fix it
- JSON vs XML: key differences and when to use each
- How to fix common XML errors
Check your own JSON or XML in seconds — free, and nothing is uploaded.
Open the formatter →