How to Convert JSON to XML

Some enterprise endpoints, older SOAP services and Java tooling still only accept XML. When your data lives in JSON, here's the shortest path across.

The Transmute CLI (free, zero dependencies)

Given users.json:

[
  { "name": "Alice", "age": 32 },
  { "name": "Bob",   "age": 20 }
]
$ transmute users.json --output xml --pipe '[{"op":"head","n":10}]'
<?xml version="1.0" encoding="UTF-8"?>
<data>
  <item>
    <name>Alice</name>
    <age>32</age>
  </item>
  ...
</data>

Because parsing, transforming and serializing are separate steps, you can reshape before emitting XML:

$ transmute users.json     --pipe '[{"op":"filter","expr":"item.age >= 21"},{"op":"sort","by":"name"}]'     --output xml

Python equivalent

import json
from dicttoxml import dicttoxml

data = json.load(open("users.json"))
print(dicttoxml(data).decode())

pip install dicttoxml. Works, but pulls in more of your dependency tree than most one-off jobs justify.

Things that bite people

  • Arrays of objects map naturally; a single bare object needs wrapping first (Transmute does this automatically).
  • Special characters in values must be escaped (&, <). A serializer handles this; string concatenation does not.
  • Nulls: decide whether they become empty elements or disappear — be consistent.