XML to JSON Namespace Mapping: Keep Same-Named Elements Separate

An inventory feed can use inv:code for a product and ship:code for a carrier. Stripping both prefixes would make the two code fields ambiguous. Convert with namespace declarations intact and resolve names using the namespace URI and local name wherever identity matters.

Worked example

Two code elements from different namespaces

<parcel xmlns:inv="urn:demo:inventory" xmlns:ship="urn:demo:shipping">
  <inv:code>SKU-17</inv:code>
  <ship:code>CARRIER-B</ship:code>
</parcel>

Steps

  1. Convert the sample with Keep attributes on and the default @_ prefix. Look for inv:code and ship:code as separate JSON keys.
  2. Check the enclosing parcel object for @_xmlns:inv and @_xmlns:ship. These declarations identify which namespace URI each prefix refers to.
  3. In code that consumes multiple feeds, resolve each element with a namespace-aware XML parser before normalizing it to JSON. Test a second feed with inv renamed to a different prefix but bound to the same URI; the product ID should still map to the same field.

What to check

  • The converter keeps the literal prefix in each key. It does not normalize names to namespace URIs or resolve a prefix rebound on an inner element for you.
  • A default namespace applies to unprefixed elements in its scope. It does not put unprefixed attributes in that namespace.

See the qualified names in the converted JSON

References