⇢

XML to JSON

Convert XML elements and attributes into JSON

XML to JSON — what it does

Convert XML elements and attributes into JSON structure, so data held in an XML document can be consumed by code and tooling that expects JSON. It is the bridge from established enterprise formats to the shape modern applications actually work with.

Worked example

A purchase order with attributes on the root and on two repeated <item> elements, converted to JSON. The <notes> element holds a CDATA section.

Input — XML

<?xml version="1.0"?><order id="A-1042" status="open"><customer>Ann Lee</customer><items><item sku="DESK-01" qty="2">Standing desk</item><item sku="ARM-04" qty="4">Monitor arm</item></items><notes><![CDATA[Deliver after 5pm]]></notes></order>

Output — JSON

{
  "order": {
    "@attributes": {
      "id": "A-1042",
      "status": "open"
    },
    "customer": "Ann Lee",
    "items": {
      "item": [
        {
          "@attributes": {
            "sku": "DESK-01",
            "qty": "2"
          },
          "#text": "Standing desk"
        },
        {
          "@attributes": {
            "sku": "ARM-04",
            "qty": "4"
          },
          "#text": "Monitor arm"
        }
      ]
    },
    "notes": "Deliver after 5pm"
  }
}

Attributes go under "@attributes", the text of an element that also has attributes goes under "#text", the two <item> elements become an array, and the CDATA text in <notes> comes out as a plain string.

When this tool helps

  • Consuming an XML API response in JavaScript or a JSON-based pipeline.
  • Migrating configuration or data from an XML format into a JSON one.
  • Inspecting a complex XML document in a shape that is easier to read and query.

How to use it

  1. Paste the XML input.
  2. Process the conversion.
  3. Review the JSON structure.
  4. Copy the result into your project.

Accuracy, limits, and good practice

  • XML and JSON do not map cleanly: attributes, text content and child elements all have to be represented somehow, so check the shape before relying on it.
  • A single repeated element may become an object rather than an array, which is the classic source of code that works on one payload and breaks on the next.
  • Everything arrives as strings, because XML carries no type information — cast numbers and booleans on the receiving side.

Frequently asked questions

Why does the JSON look more complicated than the XML?

Because attributes and text content need distinct representations in JSON. The extra structure is preserving information XML expresses positionally.

Are namespaces preserved?

Namespace prefixes generally survive in key names, though JSON has no namespace concept of its own.

How do I convert back?

The JSON to XML tool converts in the other direction, wrapping the result in a root element you name.

What shape does an element take in the JSON?

An element with neither attributes nor child elements becomes a plain string of its text ("customer": "Ann Lee"). With attributes it becomes an object holding "@attributes" plus "#text" for its text. Child elements become keys named after the tag; a tag that repeats under the same parent becomes an array, and text sitting between child elements is joined with spaces into a "#text" key. The declaration and comments are not represented.

Why is a single child an object but repeated children an array?

An array is created only when the same tag appears more than once under one parent. <orders> with a single <order> gives "order": {"id": "1042"}, while two <order> elements give "order": [ … ], so code that reads the result should accept both shapes. All values stay strings: <id>1042</id> and qty="2" are not converted to numbers.

Related tools on this site

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

XML 轉 JSON能做什麼

把 XML 的元素與屬性轉成 JSON 結構,讓 XML 文件中的資料能被期待 JSON 的程式與工具使用。它是從既有企業格式,通往現代應用實際使用的資料形狀的橋樑。

實際範例

一份根元素與兩個重複的 <item> 元素都帶屬性的採購單,轉成 JSON。<notes> 元素裡放的是 CDATA 區段。

來源:XML

<?xml version="1.0"?><order id="A-1042" status="open"><customer>Ann Lee</customer><items><item sku="DESK-01" qty="2">Standing desk</item><item sku="ARM-04" qty="4">Monitor arm</item></items><notes><![CDATA[Deliver after 5pm]]></notes></order>

轉換後:JSON

{
  "order": {
    "@attributes": {
      "id": "A-1042",
      "status": "open"
    },
    "customer": "Ann Lee",
    "items": {
      "item": [
        {
          "@attributes": {
            "sku": "DESK-01",
            "qty": "2"
          },
          "#text": "Standing desk"
        },
        {
          "@attributes": {
            "sku": "ARM-04",
            "qty": "4"
          },
          "#text": "Monitor arm"
        }
      ]
    },
    "notes": "Deliver after 5pm"
  }
}

屬性放在「@attributes」底下,同時帶屬性的元素其文字放在「#text」底下,兩個 <item> 變成陣列,<notes> 裡的 CDATA 文字則以一般字串呈現。

什麼時候用得上

  • 在 JavaScript 或以 JSON 為主的流程中使用 XML API 回應。
  • 把設定或資料從 XML 格式搬遷到 JSON。
  • 以更好讀、好查詢的形狀檢視複雜的 XML 文件。

使用步驟

  1. 貼上 XML 輸入。
  2. 執行轉換。
  3. 檢視 JSON 結構。
  4. 把結果複製到專案中。

精確度、限制與實務建議

  • XML 與 JSON 無法乾淨對應:屬性、文字內容與子元素都得想辦法表示,使用前請先確認結構。
  • 只出現一次的重複元素可能變成物件而非陣列,這正是「這份載荷可以、下一份就壞掉」的經典原因。
  • 所有值都會是字串,因為 XML 不帶型別資訊——請在接收端自行轉型。

常見問題

為什麼 JSON 看起來比 XML 複雜?

因為屬性與文字內容在 JSON 中需要各自的表示方式。多出來的結構,是在保留 XML 用位置表達的資訊。

命名空間會保留嗎?

命名空間前綴通常會保留在鍵名中,但 JSON 本身沒有命名空間的概念。

怎麼轉回去?

JSON 轉 XML 工具負責反方向,並會把結果包在你指定名稱的根元素中。

元素在 JSON 裡會是什麼形狀?

沒有屬性也沒有子元素的元素會直接變成其文字的字串("customer": "Ann Lee")。帶屬性時會變成一個物件,含「@attributes」以及放文字的「#text」。子元素以標籤名作為鍵;同一父元素底下重複的標籤會變成陣列,夾在子元素之間的文字會以空格接起來放進「#text」。宣告與註解不會被表示。

為什麼單一子元素是物件,重複的子元素卻是陣列?

只有同一父元素底下同一個標籤出現兩次以上時才會建立陣列。<orders> 裡只有一個 <order> 時得到 "order": {"id": "1042"},有兩個 <order> 時則是 "order": [ … ],讀取結果的程式要能接受這兩種形狀。所有值都維持字串:<id>1042</id> 與 qty="2" 都不會被轉成數字。

本站相關工具

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