Mapping rules

  • The root element is <root> by default and configurable.
  • Object keys become child elements; nested objects nest further.
  • Arrays become a wrapper element containing one <item> per value.
  • Root arrays use the configured root element and the same <item> wrapper.
  • Strings are escaped (&, <, >), numbers and booleans are text content.
  • null produces an empty element.
  • Keys starting with a digit or containing spaces are prefixed and sanitized.
xml
<root>
  <user>
    <id>1</id>
    <tags>
      <item>math</item>
      <item>code</item>
    </tags>
  </user>
</root>

Where conversion loses information

XML can represent mixed content, attributes and namespaces; JSON cannot. Going from JSON to XML is lossless for the data shapes shown here, but the reverse can lose attributes and ordering. If the target schema expects attributes rather than elements, adjust the output manually.

Some APIs require a declaration such as <?xml version="1.0" encoding="UTF-8"?>; add it if the consumer demands it.