T

TechIdea

Ecosystem

Back to Integration Hub

Converting JSON to XML

How to dynamically construct complex XML envelopes from standard JSON data to communicate with legacy SOAP APIs and ERP software.

Why do we still need XML?

If you are building a modern web application in React, Vue, or Next.js, your entire stack speaks JSON. However, the moment your app needs to talk to a banking API, a government gateway, or an enterprise ERP (like SAP or Tally), you hit a wall: they require XML payloads wrapped in SOAP envelopes.

The Challenge with Arrays

Converting JSON to XML is not a 1:1 mapping. JSON uses brackets [] to define arrays of similar items. XML has no concept of an array; it simply repeats the same tag name multiple times.

// JSON Array
{
  "users": [
    { "name": "Alice" },
    { "name": "Bob" }
  ]
}

Must be explicitly mapped to repeat the child tag in XML:

<!-- XML Repetition -->
<users>
  <user>
    <name>Alice</name>
  </user>
  <user>
    <name>Bob</name>
  </user>
</users>

Using fast-xml-parser Builder

The easiest way to generate XML programmatically in Node.js is using the XMLBuilder from the fast-xml-parser library.

const { XMLBuilder } = require('fast-xml-parser');

const jsonPayload = {
    request: {
        "@_version": "1.0",
        user: [
            { name: "Alice", role: "Admin" },
            { name: "Bob", role: "Editor" }
        ]
    }
};

const builder = new XMLBuilder({
    ignoreAttributes: false,
    format: true
});

const xmlOutput = builder.build(jsonPayload);
console.log(xmlOutput);

Handling Attributes (@_)

Notice the "@_version" key in the JSON payload above? By convention, parsers use a prefix (usually @_) to differentiate between an XML node's text content and its attributes. When the builder runs, "@_version": "1.0" becomes <request version="1.0">.


Try it yourself

Paste your JSON payload into our free converter tool to instantly see how it maps to an XML structure.

Open JSON to XML Tool

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.