⇠

JSON to XML

Convert JSON data into an XML document

JSON to XML — what it does

Convert a JSON document into XML, wrapping the result in a root element you name. Plenty of established systems — enterprise integrations, SOAP services, publishing pipelines and government filings — still speak XML, and this handles the structural translation between the two.

Worked example

Two orders converted with the root element set to orders; the first uses the @attributes key and contains a null and an array, the second has an empty array.

JSON input

{"order": [
  {"@attributes": {"id": "ORD-1042"}, "customer": "Ann Lee", "total": 129.5,
   "paid": true, "note": null, "items": ["Desk lamp", "USB cable"]},
  {"@attributes": {"id": "ORD-1043"}, "customer": "Mark & Co", "total": 40,
   "paid": false, "items": []}
]}

Output — XML

<?xml version="1.0" encoding="UTF-8"?>
<orders>
  <order id="ORD-1042">
    <customer>Ann Lee</customer>
    <total>129.5</total>
    <paid>true</paid>
    <note></note>
    <items>Desk lamp</items>
    <items>USB cable</items>
  </order>
  <order id="ORD-1043">
    <customer>Mark &amp; Co</customer>
    <total>40</total>
    <paid>false</paid>
  </order>
</orders>

Each element with children starts its children on a new line; null becomes an empty <note></note>, the empty items array of ORD-1043 produces no element at all, and & is escaped.

When this tool helps

  • Feeding JSON data into a legacy service that only accepts XML input.
  • Producing an XML payload for a SOAP endpoint from data you already hold as JSON.
  • Converting configuration or content between two systems with different format preferences.

How to use it

  1. Paste the JSON input.
  2. Enter the name for the root element.
  3. Process the conversion.
  4. Copy the generated XML.

Accuracy, limits, and good practice

  • XML needs a single root element, which JSON does not — that is why naming one is a required step rather than an option.
  • JSON keys that are not valid XML names, such as those starting with a digit or containing spaces, are renamed automatically (spaces and punctuation become _, a leading digit gets a leading _); rename them yourself first if the receiving system expects specific names.
  • JSON arrays have no direct XML equivalent; they become repeated elements, which is the conventional representation.
  • Pick a root element name that describes the payload, not the tool: it is the first thing anyone reading the XML sees.

Frequently asked questions

Do JSON types survive the conversion?

No. XML element content is text, so numbers and booleans become their string representations and typing is up to the schema.

Can I produce attributes instead of elements?

Yes, for the keys you choose: put them in an "@attributes" object, as in {"order": {"@attributes": {"id": "ORD-1042"}}}, and they are written as id="ORD-1042" on that element. Every other key becomes a child element.

How do I go the other way?

The XML to JSON tool converts elements and attributes back into JSON structure.

What happens to null values?

They typically become empty elements. Confirm how the receiving system distinguishes an empty element from a missing one.

What if the JSON's top level is an array?

The array is wrapped in the root element and each entry becomes an <item> child, so ["a","b"] with the root box left blank gives <root> containing <item>a</item> and <item>b</item>, and an empty [] gives just <root/>. An array inside an object repeats its key instead, as <items> does above; a nested array keeps its structure: {"matrix":[[1,2],[3]]} gives two <matrix> elements, the first holding <item>1</item><item>2</item> and the second <item>3</item>.

What happens to keys that are not valid XML names?

They are cleaned the same way for elements and for @attributes keys: spaces and punctuation become _, and a name that does not start with a letter or _ gets a leading _. "1st item" becomes <_1st_item>, and {"@attributes": {"due date": "2026-11-05", "1st-ref": "A&B"}} is written as due_date="2026-11-05" _1st-ref="A&amp;B" — hyphens are kept because they are legal in XML names, and & in the value is escaped.

Related tools on this site

Everything above runs inside this page. JSON to XML needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

JSON 轉 XML能做什麼

把 JSON 文件轉成 XML,並包在你指定名稱的根元素中。許多既有系統——企業整合、SOAP 服務、出版流程與政府申報——仍以 XML 溝通,這個工具負責兩者之間的結構轉譯。

實際範例

兩筆訂單以根元素 orders 轉換;第一筆有 @attributes 鍵、一個 null 與一個陣列,第二筆則有一個空陣列。

原文:JSON

{"order": [
  {"@attributes": {"id": "ORD-1042"}, "customer": "Ann Lee", "total": 129.5,
   "paid": true, "note": null, "items": ["Desk lamp", "USB cable"]},
  {"@attributes": {"id": "ORD-1043"}, "customer": "Mark & Co", "total": 40,
   "paid": false, "items": []}
]}

轉換後:XML

<?xml version="1.0" encoding="UTF-8"?>
<orders>
  <order id="ORD-1042">
    <customer>Ann Lee</customer>
    <total>129.5</total>
    <paid>true</paid>
    <note></note>
    <items>Desk lamp</items>
    <items>USB cable</items>
  </order>
  <order id="ORD-1043">
    <customer>Mark &amp; Co</customer>
    <total>40</total>
    <paid>false</paid>
  </order>
</orders>

有子元素的元素,其子元素都從新的一行開始;null 變成空的 <note></note>,ORD-1043 的空陣列 items 完全不產生元素,& 則被跳脫。

什麼時候用得上

  • 把 JSON 資料送進只接受 XML 的舊系統。
  • 用手上已有的 JSON 資料,為 SOAP 端點產生 XML 載荷。
  • 在兩個偏好格式不同的系統之間轉換設定或內容。

使用步驟

  1. 貼上 JSON 輸入。
  2. 輸入根元素名稱。
  3. 執行轉換。
  4. 複製產生的 XML。

精確度、限制與實務建議

  • XML 需要單一根元素,JSON 則不需要——這就是「命名根元素」是必要步驟而非選項的原因。
  • 不是合法 XML 名稱的鍵(例如以數字開頭或含空格)會自動改名:空格與標點變成 _,開頭是數字時前面補 _。若接收端要求特定名稱,請在轉換前自行改名。
  • JSON 陣列在 XML 中沒有直接對應,會變成重複出現的元素,這是慣用表示法。
  • 根元素請取能描述內容的名稱,而不是工具名:那是任何人打開 XML 第一眼看到的東西。

常見問題

JSON 的型別會保留嗎?

不會。XML 元素內容是文字,數字與布林值會變成字串形式,型別由 Schema 決定。

可以輸出成屬性而不是元素嗎?

可以,針對你指定的鍵:把它們放進「@attributes」物件,例如 {"order": {"@attributes": {"id": "ORD-1042"}}},就會在該元素上寫成 id="ORD-1042"。其他鍵一律成為子元素。

反方向怎麼轉?

XML 轉 JSON 工具能把元素與屬性轉回 JSON 結構。

null 值會怎樣?

通常會變成空元素。請確認接收系統如何區分「空元素」與「元素不存在」。

JSON 最外層是陣列會怎樣?

陣列會包進根元素,每個項目變成一個 <item> 子元素,所以根名稱留空時 ["a","b"] 會得到 <root>,裡面是 <item>a</item> 與 <item>b</item>;空的 [] 則只得到 <root/>。物件裡的陣列以鍵名重複,如上例的 <items>;巢狀陣列會保留結構:{"matrix":[[1,2],[3]]} 得到兩個 <matrix>,第一個內含 <item>1</item><item>2</item>,第二個內含 <item>3</item>。

不是合法 XML 名稱的鍵會怎麼處理?

元素名稱與 @attributes 裡的鍵都以同樣方式整理:空格與標點變成 _,不是以字母或 _ 開頭的名稱前面會補一個 _。「1st item」會變成 <_1st_item>,{"@attributes": {"due date": "2026-11-05", "1st-ref": "A&B"}} 則寫成 due_date="2026-11-05" _1st-ref="A&amp;B":連字號在 XML 名稱裡合法所以保留,值裡的 & 會被跳脫。

本站相關工具

以上步驟全部在這個頁面內完成,JSON 轉 XML 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。