Unlike XML β JSON (which NetSuite can handle with N/xml), converting JSON β XML in SuiteScript has no built-in function.
This means you must manually:
- Parse the JSON
- Recursively build XML nodes
- Escape XML-unsafe characters
- Handle nested objects and arrays
This guide walks you through a clean, reusable approach that works in SuiteScript 2.0 / 2.1, and is ideal for integrations like:
- EDI mappings
- SOAP API requests
- Legacy ERP connections
- XML-based 3PL integrations
- Custom export files
1. Parse the JSON String
If your JSON data comes as a string (common in API responses or file imports), start by parsing it:
var jsonString = '{"name": "John Doe", "age": 30, "city": "New York"}';
var jsonObject = JSON.parse(jsonString);
This gives you a normal JavaScript object that can be converted to XML.
2. Core JSON β XML Conversion Function
Below is a reusable SuiteScript-friendly function that converts:
- Simple key/value pairs
- Nested objects
- Arrays of objects
- Arrays of primitive values
- Mixed structures
SuiteScript JSON-to-XML Converter
function jsonToXml(obj, rootName) {
var xml = '<' + rootName + '>';
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
// Nested object
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
xml += jsonToXml(value, key);
// Array of values or objects
} else if (Array.isArray(value)) {
value.forEach(function (item) {
xml += jsonToXml(item, key);
});
// Primitive values
} else {
xml += '<' + key + '>' + escapeXml(String(value)) + '</' + key + '>';
}
}
}
xml += '</' + rootName + '>';
return xml;
}
3. Escape XML Special Characters
XML cannot contain these raw characters:
| Character | Must Become |
|---|---|
< | < |
> | > |
& | & |
' | ' |
" | " |
Hereβs the function to safely escape them:
function escapeXml(unsafe) {
return unsafe.replace(/[<>&'"]/g, function(c) {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
Use this before adding any value to XML.
4. Full Working Example
var jsonString = '{"name":"John Doe","age":30,"city":"New York"}';
var jsonObject = JSON.parse(jsonString);
var xmlString = jsonToXml(jsonObject, 'RootElement');
// Output:
// <RootElement>
// <name>John Doe</name>
// <age>30</age>
// <city>New York</city>
// </RootElement>
You can now send xmlString in a SOAP request or store it in NetSuite.
5. Handling Complex JSON Structures
This solution supports:
β Nested JSON Objects
{
"employee": {
"name": "John",
"address": {
"street": "123 Main",
"city": "Toronto"
}
}
}
β Arrays
{
"items": [
{ "sku": "A01", "qty": 2 },
{ "sku": "A02", "qty": 1 }
]
}
Result:
<Root>
<items><sku>A01</sku><qty>2</qty></items>
<items><sku>A02</sku><qty>1</qty></items>
</Root>
β Mixed Types
Works automatically.
Important Considerations
1. XML Schema Requirements
If your target system has strict XML formatting rules (EDI, banking, government APIs), adjust:
- Element names
- Attribute format
- Node ordering
2. Performance
For very large JSON payloads (e.g., 10,000 lines):
- Use Map/Reduce
- Avoid unnecessary recursion
- Build strings efficiently
3. Data Integrity
Always apply escapeXml() to avoid malformed XML.
Where This Is Used in Real Integrations
β EDI (850, 856, 810, 870)
β Salesforce β NetSuite custom XML exports
β Boomi and Celigo hybrid flows
β 3PL shipping label integrations
β Vendor/bank API payloads
β Custom XML audit logs
β Legacy ERP data migrations
Related Guides (Add Internal Links)
Discover more from The NetSuite Pro
Subscribe to get the latest posts sent to your email.
Leave a Reply