JSON to XML API

Convert JSON to XML, escaping entities and repairing illegal element names.

EndpointPOST https://astraljson.com/api/v1/json-to-xml
AuthBearer token, free
Sendsinput: text
Returnsresult: text
PriceFree. 1,000 calls a day, 60 a minute.

When to call it

XML is usually somebody else's requirement: a SOAP endpoint, a bank file, an enterprise system that will outlive the service talking to it. Converting in code rather than by hand is what keeps the output valid when a value suddenly contains an ampersand, which is exactly the case that breaks a hand-rolled converter.

For a one-off, the browser version needs no token and never sends your data anywhere: it runs on your own machine.

Example

curl
curl -X POST https://astraljson.com/api/v1/json-to-xml \
  -H "Authorization: Bearer ast_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"input": "{\"order id\":\"A&B\",\"total\":19.9,\"items\":[{\"sku\":\"x<1\"}]}"}'
response
{
  "tool": "json-to-xml",
  "result": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n  <order_id>A&amp;B</order_id>\n  <total>19.9</total>\n  <items>\n    <sku>x&lt;1</sku>\n  </items>\n</root>",
  "chars": 149,
  "ms": 2
}

In your language

JavaScript
const res = await fetch("https://astraljson.com/api/v1/json-to-xml", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ASTRAL_TOKEN}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({"input": "{\"order id\":\"A&B\",\"total\":19.9,\"items\":[{\"sku\":\"x<1\"}]}"}),
});
if (!res.ok) throw new Error((await res.json()).detail);
const { result } = await res.json();
Python
import os, requests

res = requests.post(
    "https://astraljson.com/api/v1/json-to-xml",
    headers={"Authorization": f"Bearer {os.environ['ASTRAL_TOKEN']}"},
    json={"input": "{\"order id\":\"A&B\",\"total\":19.9,\"items\":[{\"sku\":\"x<1\"}]}"},
    timeout=30,
)
res.raise_for_status()
result = res.json()["result"]

Parameters

FieldTypeNotes
inputstringRequired. Up to 200,000 characters per call.

Errors

Errors are RFC 7807 problem documents with a stable type you can branch on.

missing-token401No Authorization header.
bad-request400input missing or the wrong type.
tool-failed422The input could not be processed. detail says why.
quota-exceeded429Past 1,000 calls today.

Questions

Are XML entities escaped?
Yes. Ampersand, less-than, greater-than, quote and apostrophe are escaped in every text value, so the result parses. A converter that skips this produces documents no XML reader will accept.
What happens to a key that is not a legal element name?
Illegal characters become underscores and a name that cannot start where it does gets an underscore in front, so "order id" becomes order_id and "1st" becomes _1st.
How are arrays represented?
An array of scalars repeats the element once per value. A top-level array is wrapped in root with one item element per entry.

Other endpoints

A token takes thirty seconds

Free, no card, 1,000 calls a day, and the same token works on astraltext.com, astralpdf.com, astraljson.com and astralbatch.com.

Get a free token