XML ↔ JSON / TOML / INI API
Convert XML to JSON and back, check XML well-formedness with line and column, pretty-print or minify XML, and convert TOML or INI to and from JSON.
Send a document in xml, json, toml or ini (or as the raw body with from) and pick to: json, xml, toml, ini, validate, pretty or minify. XML to JSON keeps attributes under a prefix (@_ by default, attribute_prefix=none for none), puts mixed text in #text, and can force arrays for repeated elements (array_tags=item,line or always_array=true), keep the element order (preserve_order=true), strip namespace prefixes (remove_ns=true) and keep CDATA or comments. Values stay strings unless parse_values=true, so tax codes and invoice numbers such as 0101234567 keep their leading zeros.
JSON to XML turns keys starting with the prefix into attributes, arrays into repeated elements and wraps the result in root when needed; the output is checked again and a warning is returned if a key is not a valid element name. validate reports the first error with its line and column, plus the root element, namespaces and element count; pretty / minify re-indent XML (or JSON) keeping comments, CDATA and processing instructions.
Safety: entity expansion is limited (no "billion laughs"), external entities (SYSTEM / PUBLIC) are never loaded, nesting is capped at 256 levels and documents at 5 MB. TOML support covers TOML 1.0 except arbitrary-precision integers (dates and times become strings, inf / nan become strings); INI supports sections, ; / # comments, quoted values, key[] arrays and optional dotted sections.
Parameters
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
xml | body | string | no | XML document (or send json, toml, ini, input + from, or the raw body). | |
json | body | object | no | JSON value (object or JSON string) to convert. | |
toml | body | string | no | TOML document. | |
ini | body | string | no | INI document. | |
from | body | string | no | xml, json, toml or ini (detected when omitted). | |
to | body | string | no | json | json, xml, toml, ini, validate, pretty or minify (default json, or xml for JSON input). |
attribute_prefix | body | string | no | @_ | Prefix of attribute keys; none for no prefix. |
attributes | body | boolean | no | true | false drops attributes. |
text_node_name | body | string | no | #text | Key of the text of elements that also have attributes or children. |
array_tags | body | string | no | Comma-separated element names always returned as arrays. | |
always_array | body | boolean | no | false | Every element below the root becomes an array. |
parse_values | body | boolean | no | false | Convert numbers and booleans (XML, INI). Leading zeros are kept as strings. |
preserve_order | body | boolean | no | false | Ordered array output (exact element order, mixed content). |
remove_ns | body | boolean | no | false | Remove namespace prefixes (inv:Total → Total). |
keep_cdata | body | boolean | no | false | Keep CDATA sections under #cdata. |
keep_comments | body | boolean | no | false | Keep comments under #comment. |
root | body | string | no | root | JSON to XML: name of the wrapping root element. |
indent | body | number | no | 2 | Indentation for XML / JSON output (0 = one line). |
declaration | body | boolean | no | true | JSON to XML: add <?xml version="1.0" encoding="UTF-8"?>. |
nested_sections | body | boolean | no | false | INI: [a.b] becomes nested objects. |
Example
curl -X POST "https://apibox.io.vn/v1/xml-convert" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"xml":"<invoice id=\"HD001\"><buyer>Công ty A</buyer><item><name>Pen</name><qty>2</qty></item><item><name>Book</name><qty>1</qty></item></invoice>"}'const res = await fetch('https://apibox.io.vn/v1/xml-convert', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"xml": "<invoice id=\"HD001\"><buyer>Công ty A</buyer><item><name>Pen</name><qty>2</qty></item><item><name>Book</name><qty>1</qty></item></invoice>"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/xml-convert",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"xml": "<invoice id=\"HD001\"><buyer>Công ty A</buyer><item><name>Pen</name><qty>2</qty></item><item><name>Book</name><qty>1</qty></item></invoice>"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/xml-convert');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"xml":"<invoice id=\\"HD001\\"><buyer>Công ty A</buyer><item><name>Pen</name><qty>2</qty></item><item><name>Book</name><qty>1</qty></item></invoice>"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Response
{
"from": "xml",
"to": "json",
"root": "invoice",
"json": {
"invoice": {
"buyer": "Công ty A",
"item": [
{
"name": "Pen",
"qty": "2"
},
{
"name": "Book",
"qty": "1"
}
],
"@_id": "HD001"
}
}
}
Use cases
- Read e-invoice, tax return or social-insurance XML files as JSON
- Send JSON data to a legacy SOAP / XML system
- Validate XML uploaded by users and show the exact error line
- Convert config files between TOML, INI and JSON
FAQ
Why are numbers returned as strings?
XML has no types, and codes like 0101234567 must keep their leading zeros. Add parse_values=true to get numbers and booleans.
How do I always get an array for repeated elements?
List them in array_tags (e.g. array_tags=HHDVu), or use always_array=true for every element below the root.
Is DOCTYPE / entity processing safe?
Yes: internal entities are expanded only within small limits, nested expansions beyond them stay as text, and external entities are rejected.
Related APIs
JSON Validator
Check JSON syntax with exact line and column of errors, and validate data against a JSON Schema (draft 4, 7, 2019-09 or 2020-12).
{ }JSON Formatter
Pretty print JSON with 0-8 spaces or tabs, minify it, or sort object keys recursively. Syntax errors are reported with line and column.
CSVCSV to JSON
Convert CSV into an array of JSON objects using the header row, with delimiter auto-detection, number and boolean typing, and row-level errors.