Turn a JSON object into an indented XML document. Keys starting with @ become attributes, a #text key becomes the element's text, and arrays repeat an element with the same name, so plain JSON is enough to describe XML that has attributes.
It fits situations where you hold data as JSON but must send XML: a SOAP request body, an RSS feed or sitemap, or a legacy payment, logistics or government API that only accepts XML.
JSON box. Make the top level an object with a single key, like {"rootName": {...}}, and that key becomes the root element.@ such as "@id": "1"; to combine attributes with text content, use a "#text" key.Result, including the <?xml ...?> declaration, and copy it.{
"rss": {
"@version": "2.0",
"channel": {
"title": "Release notes",
"item": [
{ "title": "v1.2 & fixes", "guid": { "@isPermaLink": "false", "#text": "r-12" } },
{ "title": "v1.3", "guid": { "@isPermaLink": "false", "#text": "r-13" } }
]
}
}
}<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Release notes</title>
<item>
<title>v1.2 & fixes</title>
<guid isPermaLink="false">r-12</guid>
</item>
<item>
<title>v1.3</title>
<guid isPermaLink="false">r-13</guid>
</item>
</channel>
</rss>
@version and @isPermaLink became attributes, and the #text of guid became the element's content. The item array is written as two <item> elements, and the & in the title is escaped to &.
<?xml version="1.0" encoding="UTF-8"?> declaration and indents with two spaces.<root>, with each array entry written as <item>.&, <, >, " and ' as entities._, and keys starting with a digit get a leading _: first name → first_name, 2nd → _2nd.null values are written as empty elements like <tag/>.Hand-writing tags from a WSDL sample makes it easy to mismatch an opening and closing tag. Write the request data as JSON, convert it, and tag pairs and escaping come out right.
Sketch the feed your server should produce as JSON, check which keys turn into attributes and which into repeated elements, then port that shape to code.
[]) has nothing to repeat and disappears from the output.<price>12.5</price>, with no XML Schema type information."soap:Envelope" and an "@xmlns:soap" attribute yourself and they are emitted as given.Wrap your data in an object with exactly one key, e.g. {"order": {...}}. If there are several keys, or that single value is an array, <root> is used instead.
Give an array: {"tag": ["a", "b"]} becomes <tag>a</tag><tag>b</tag>. For a <tags> wrapper around them, use {"tags": {"tag": [...]}}.
Unicode letters such as Korean or accented characters are valid in XML names and kept as is. Spaces, /, and @ anywhere but the start are replaced with _, so confirm the tag names match what the receiver expects.