XML Has Concepts JSON Does Not
The two models do not line up one-to-one, so any conversion has to make choices. Three differences matter in practice:
- Attributes vs child elements. XML distinguishes
<user id="1"/>from<user><id>1</id></user>; JSON has only keys, so attributes are folded in alongside child elements. - Repetition is implicit. One
<item>looks like a single value, two look like an array. A document with exactly one item therefore produces an object where your code may expect a one-element array — normalise on the consuming side. - Mixed content. Text interleaved with child elements, as in
<p>see <b>this</b> now</p>, has no natural JSON shape and does not survive cleanly.
Namespaces
Prefixes such as soap:Envelope are kept as part of the key name, since JSON has no namespace mechanism to move them into. Expect keys containing a colon, and quote them when you reference them in code.
On the Command Line
# Python, using xmltodict
python3 -c "import sys,xmltodict,json; json.dump(xmltodict.parse(sys.stdin.read()), sys.stdout, indent=2)" < in.xml
# Node.js, using fast-xml-parser
node -e "const{XMLParser}=require('fast-xml-parser');console.log(JSON.stringify(new XMLParser().parse(require('fs').readFileSync(0,'utf8')),null,2))" < in.xmlWho this guide is for
- Turn a SOAP response into JSON your frontend can consume
- Convert an RSS or Atom feed into JSON for a reader UI
- Migrate a legacy XML config file to JSON
- Inspect a verbose XML payload in a more compact shape