All ArticlesData Formats & Parsing

XML vs JSON in Enterprise Systems: XPath, Schema Validation, and Transformation

Vijay Aravindh
· Last updated:
5 min read

TL;DR

Understand XML and JSON coexistence in modern architectures. Master XPath queries, XSD schema validation, and bidirectional data transformation.

Try the Interactive Tool

Convert XML with one item and with two items, then compare the JSON shapes before writing a consumer.

Open XML to JSON Converter →

While JSON has become the ubiquitous format for modern REST APIs and single-page applications, XML (Extensible Markup Language) remains deeply entrenched in enterprise infrastructure, legacy banking networks (SWIFT, ISO 20022), SOAP web services, and metadata standards (SAML, RSS).


1. Structural Comparison: XML vs JSON

Feature JSON (RFC 8259) XML (W3C Standard)
Data Types String, Number, Boolean, Null, Array, Object Text-only (types defined via XSD)
Attributes Not supported (key-value only) First-class element attributes
Namespaces No native support Fully supported (xmlns)
Query Language JSONPath / JMESPath XPath / XQuery (mature W3C standards)
Validation JSON Schema XSD (XML Schema Definition), DTD

2. Navigating Documents with XPath

XPath provides expressive query syntax for traversing XML document trees:

Worked example

<catalog>
  <book id="bk101" category="technical">
    <title>Designing Data-Intensive Applications</title>
    <price currency="USD">45.00</price>
  </book>
  <book id="bk102" category="fiction">
    <title>Neuromancer</title>
    <price currency="USD">14.99</price>
  </book>
</catalog>
  • Extract all technical book titles: /catalog/book[@category='technical']/title/text()
  • Find books priced over $20: //book[price > 20]/title

3. Bidirectional Transformation

When integrating legacy SOAP services with modern React frontends, converting between XML and JSON is a daily task. Test your transformations client-side with our XML to JSON Converter, JSON to XML Converter, and XPath Tester.

Convert a supplier record without losing its shape

Imagine a supplier sends an order feed whose item identifier is an attribute and whose destination code is element text. The sender documents that the code is text even when it contains only digits. Use a fixture with leading zeroes so a conversion that accidentally treats it as a number fails visibly. Paste this source into XML to JSON Converter.

<feed xmlns="urn:orders"><item id="A"><zip>007</zip></item></feed>

The converter emits this JSON for that input.

{
  "feed": {
    "item": {
      "zip": "007",
      "@_id": "A"
    },
    "@_xmlns": "urn:orders"
  }
}

The attribute has a prefix in the converted keys, while the child element becomes a normal property. Those names describe this converter's mapping. They are not a standard convention that every XML to JSON library uses. Check the contract at both ends before treating the output as a portable representation. A converter that discards attributes altogether would lose the item identifier while leaving a superficially plausible object.

Now paste a second record into the same feed.

<feed xmlns="urn:orders"><item id="A"><zip>007</zip></item><item id="B"><zip>009</zip></item></feed>
{
  "feed": {
    "item": [
      {
        "zip": "007",
        "@_id": "A"
      },
      {
        "zip": "009",
        "@_id": "B"
      }
    ],
    "@_xmlns": "urn:orders"
  }
}

A common mistake is writing a client against whichever fixture happened to arrive first. If it receives one item while expecting an array, calling map will fail. If it expects an object and receives several items, a lookup such as item.zip returns undefined. Normalize the cardinality at the boundary once, then make callers consume the normalized shape. The following adapter handles these two outputs and treats an absent item as an empty collection. It assumes the input has already passed the expected shape checks.

function orderItems(feed) {
  const item = feed.item;
  return item == null ? [] : Array.isArray(item) ? item : [item];
}

Before committing to the empty collection behavior, ask whether the provider sends an empty feed, omits a field on error, or uses a separate status element. Those cases can mean different things to a downstream importer. Keep fixtures for the zero, one, and many cases in the integration tests. A mapper that silently returns no items for a changed envelope can look like a successful empty import and cause a scheduled job to delete or skip records. Check the root and the response status before accepting the collection.

Fix an XPath that finds nothing

The same feed has a default namespace. XPath 1.0 treats unprefixed element tests as names in no namespace, so the expression below gives no result in XPath Tester, even when the elements are visible in the source.

/feed/item
No match

Use a namespace-aware path. In application code, bind a prefix to the namespace URI using the evaluator's namespace resolver and use that prefix on the element names. In the site's expression editor you can also match the namespace URI explicitly. On the two-item input above this returns the two attribute nodes shown here.

/*[local-name()='feed' and namespace-uri()='urn:orders']/*[local-name()='item' and namespace-uri()='urn:orders']/@id
 id="A"
 id="B"

The unprefixed attribute id remains outside the default namespace. That is why the query can finish with @id. Avoid a selector that only checks local-name when the document can contain two vocabularies with the same element names. A feed wrapper may include embedded metadata from another system, and matching by local name alone can silently select records in the wrong vocabulary. In a debugging session, first confirm the root namespace in the source. Then query the smallest branch you expect and compare the returned IDs with a known fixture before widening the expression.

Keep XML where its structure carries meaning

A product data feed is often straightforward to translate. A contract containing mixed content is harder. In a paragraph with text, an emphasis element, and more text, the order of those pieces matters. A JSON object with keys named text and emphasis does not necessarily retain the interleaving. If you must represent mixed content in JSON, design an ordered sequence of typed nodes or retain the source XML as part of the contract.

Well-formed XML can still violate a partner's XSD. For example, a date may use the wrong representation or an element may appear out of the order required by the schema. Use the partner's schema and validation pipeline where that compatibility matters. An XML signature can depend on canonicalized XML and declared transforms. Converting signed material to another syntax will not preserve those signed bytes or their canonical form. Verify the signature on the XML through the agreed signature pipeline before deriving any JSON data from it.

For a SOAP integration or older enterprise system, leave the external envelope in XML when the other party's contract requires it. A JSON adapter can still simplify internal calls. Put the translation at a documented boundary, keep the source and normalized fixtures next to each other, and capture a representative fault response as well as successful records. That is where differences such as attributes, namespaces, and absent elements are easiest to review before a contract changes.

Found this guide helpful?

Explore our 50+ privacy-first developer utility tools.

Explore Tools