CSV, JSON, YAML, XML and Markdown Tables: When Each Fits and How to Convert

What CSV, JSON, YAML, XML and Markdown tables can express, where conversions lose types or structure, and a five-row contacts file taken out and back.

Five formats carry most of the structured data that passes through an office: CSV from exports and spreadsheets, JSON from web services and scripts, YAML from configuration files, XML from older systems and invoicing standards, and Markdown tables from wikis and tickets. Converting between them is routine, and most of the damage happens quietly: a salary becomes text, a badge number loses its leading zero, a date turns into a timestamp, a single record stops being a list. This guide sets out what each format can express, where the conversions lose information, and which tool on this site does which step, using one five-row contacts table throughout.

What each format can and cannot say

FormatTypesNestingOrderCommentsTypical source
CSVNone: every cell is textNone: one flat tableRows and columnsNoSpreadsheet exports, databases
JSONString, number, boolean, nullObjects and arrays, any depthArrays ordered; object keys by conventionNoAPIs, scripts
YAMLAs JSON, plus datesAs JSONAs JSONYes (#)Configuration files
XMLNone: text, plus attributesElements, any depthElements orderedYesLegacy systems, e-invoicing
Markdown tableNoneNoneRows and columnsNoDocumentation, tickets

Two rules follow. Going from a richer format to a poorer one (JSON to CSV, YAML to Markdown) loses types or structure, and the only question is whether you lose them knowingly. Going the other way (CSV to JSON) does not add information: the tool cannot know that 52000 was a number, so it stays a string unless something guesses.

The running example

The file is contacts-5.csv: five staff records with a name, an email, a department, an ISO date and a salary.

name,email,department,start_date,salary
Ann Lee,ann.lee@example.com,Sales,2021-03-15,52000
Ben Ortiz,ben.ortiz@example.com,Engineering,2019-11-02,88000
Chloe Park,chloe.park@example.com,Sales,2023-06-01,47500
Dev Patel,dev.patel@example.com,Support,2020-01-20,41000
Emma Rossi,emma.rossi@example.com,Engineering,2022-09-12,91000

CSV: quotes, commas and the byte-order mark

CSV has one rule that matters: a field containing a comma, a quote or a line break is wrapped in double quotes, and a quote inside it is doubled. The CSV to JSON converter follows that rule when reading. A notes column containing "Prefers calls, not email" came out as one value with the comma intact, "Says ""remote only""" came out as Says "remote only", and a row that ended with an empty field produced "note": "" rather than a missing key. What it does not do is guess types: every value in the output is a string, including "salary": "52000" and "start_date": "2021-03-15".

Files saved from Excel as “CSV UTF-8” begin with a byte-order mark, three invisible bytes. When the browser reads a chosen file as text it drops the mark, so a BOM-prefixed copy of the contacts file produced the same JSON with the first key "name", not a corrupted one. The mark matters in the other direction: Excel relies on it to open accented text correctly, which is why HTML Table to CSV and the single-sheet export of Excel to CSV write it, and why a CSV from JSON to CSV, which does not, can show mangled characters when opened by double-clicking.

JSON: types that CSV never had

JSON distinguishes 52000 from "52000", and a script receiving the second will sort salaries alphabetically. The JSON Formatter is the place to check a document before it goes anywhere. Its Validate button reported Valid JSON for a correct array and Error: Unexpected token ']' for the same array with a trailing comma after the last object, the commonest hand-editing mistake. A badge number written as 007 failed with Unexpected number, because JSON numbers cannot have leading zeros; it has to be "007". And a seventeen-digit identifier, 90071992547409931, was formatted back as 90071992547409940: JSON numbers are read as double-precision floats, so long numeric IDs must be strings too. Format and Minify switch between the two-space indented layout and a single line; neither changes the data.

YAML: the same data, with dates and surprises

Anything the JSON to YAML converter produces can be read back. Given the contacts array as strings it wrote start_date: '2021-03-15' and salary: '52000' with quotes, because without them a YAML reader would turn one into a date and the other into a number; given the same array with salary as a JSON number it wrote salary: 52000 bare. The quoting is the converter telling you what the types were.

Reading YAML written by hand is where values change under you. The YAML to JSON converter, given start_date: 2021-03-15 unquoted, returned "2021-03-15T00:00:00.000Z", a timestamp in UTC; the quoted '2019-11-02' on the next record stayed as typed. badge: 007 became 7, '012' stayed "012", salary: 52000 became a number, and remote: yes stayed the string "yes", since the converter follows the YAML 1.2 rules in which only true and false are booleans. Quote anything that must stay text.

XML: elements, attributes and the one-child trap

XML has no numbers either, but it has two places to put a value, element text and attributes, and the converters need a convention for both. The JSON to XML converter uses a root element name you type, and treats a key called @attributes as the element's attributes and #text as its text. Wrapping the first two contacts as {"contact": [ … ]} with the root set to contacts produced <contacts> containing two <contact> elements with one child element per field; & and < inside a note were escaped. Handing it a bare array instead, with the root set to contacts, produced one <contacts> element holding a generic <item> per record, so the document is well-formed but the records lose their name; wrap the array in an object when the element name matters. Every element with children starts its children on a new line, and the XML Formatter re-indents a document while keeping its <?xml?> declaration.

The XML to JSON converter reverses the convention: attributes land under "@attributes" and all values are strings. The trap is cardinality. Two <contact> elements became a "contact" array; one <contact> element became a "contact" object, because the converter cannot know a single child was meant to be a list. A script that loops over contacts.contact works on the two-record file and fails on the one-record file. Check the shape whenever a feed can contain a single item.

Nested JSON into a flat CSV

CSV has no room for an address object inside a contact. Sent straight to JSON to CSV, a record with "address": {"city": "Taipei"} produced the cell [object Object], which is the converter writing the only text it has for a nested value. Flatten first: the JSON Flattener turned address.city, address.postcode, phones.0 and phones.1 into top-level keys, with a choice of dot or underscore as the separator, and kept true and null as they were. Then convert.

JSON to CSV builds its header from the union of keys across all objects, in the order first seen, and leaves a cell empty where an object lacks the key: two records with different fields produced the header name,department,address,note,phone, with the phone cell empty on the first row and address and note empty on the second. Nothing warns you; read the header before trusting the file.

Markdown tables and HTML tables

A Markdown table is a picture of data, not data. CSV to Markdown Table writes the contacts file as the pipe table shown in the walkthrough and downloads it as table.md; a pipe inside a cell is escaped as \|, and nothing else is changed. It is the right end point for a wiki page and the wrong one for anything that will be parsed again.

An HTML table on an intranet page is often the only copy of a figure. HTML Table to CSV takes pasted page source and a table index (0 for the first table) and produces a CSV from the cell text. A three-row salary table came back as Ann Lee,Sales,"52,000": the thousands separator in the page is kept and the cell is quoted because of it, so the column is text until a spreadsheet re-reads it. The status counted 4 row(s), header included.

Excel in and out

Excel adds two things CSV lacks: typed cells and more than one sheet. CSV to Excel takes a CSV file and a sheet name and downloaded contacts-5.xlsx with the sheet named Contacts; the spreadsheet library decides the cell types from the text, much as Excel does when it opens a CSV, so check a badge or postcode column afterwards. Excel to CSV lists the sheets of a workbook (here Contacts and Notes, reported as 2 worksheets), exports the chosen one as Contacts.csv, or every sheet as contacts-csv.zip containing Contacts.csv and Notes.csv. Date cells are written as Excel displays them: a start-date column formatted YYYY-MM-DD came out as 2021-03-15, and a column left in a regional format would come out in that format.

Walkthrough: contacts, out and back

  1. CSV to JSON. Choosing contacts-5.csv produced an array of five objects, every value a string. The first:
    {
      "name": "Ann Lee",
      "email": "ann.lee@example.com",
      "department": "Sales",
      "start_date": "2021-03-15",
      "salary": "52000"
    }
  2. CSV to Markdown Table. The same file produced:
    | name | email | department | start_date | salary |
    | --- | --- | --- | --- | --- |
    | Ann Lee | ann.lee@example.com | Sales | 2021-03-15 | 52000 |
    | Ben Ortiz | ben.ortiz@example.com | Engineering | 2019-11-02 | 88000 |
    | Chloe Park | chloe.park@example.com | Sales | 2023-06-01 | 47500 |
    | Dev Patel | dev.patel@example.com | Support | 2020-01-20 | 41000 |
    | Emma Rossi | emma.rossi@example.com | Engineering | 2022-09-12 | 91000 |
  3. JSON to CSV. Pasting the five-object array back produced the original header and five lines exactly; the download converted.csv was 328 bytes against the source's 329, the difference being the trailing newline. No quotes were added, because no value needed them.
  4. JSON to YAML. The same array became five - name: blocks with start_date and salary quoted, as described above.
  5. JSON to XML and back. Wrapped as {"contact": [ … ]} with root contacts, tidied in the XML Formatter, and read back through XML to JSON, the five records returned as a contact array of five objects with string values, the same shape they started with.

The round trip was lossless because the data was all text to begin with. Add one number, one nested object or one lone record, and the traps above apply.

Checklist before you convert

  • Decide which columns are numbers, dates and IDs, and quote the IDs.
  • Flatten nested JSON before it meets CSV; wrap arrays in an object before they meet XML if the elements need a real name.
  • Validate JSON after hand edits; quote dates and leading-zero values in YAML.
  • Expect a single XML child to come back as an object, not a one-item array.
  • Use a BOM-writing export when Excel is the next reader, and a plain one when a script is.

CSV、JSON、YAML、XML 與 Markdown 表格各能表達什麼、轉換時在哪裡遺失型別或結構,並以一份五列聯絡人檔案實測轉出再轉回。

辦公室裡流通的結構化資料,大多以五種格式承載:來自匯出檔與試算表的 CSV、來自網路服務與指令碼的 JSON、來自設定檔的 YAML、來自舊系統與電子發票標準的 XML,以及來自 Wiki 與工單的 Markdown 表格。在它們之間轉換是家常便飯,而大部分的損害都是悄悄發生的:薪資變成了文字、員工編號掉了開頭的零、日期變成時間戳記、單筆紀錄不再是清單。本文說明每種格式能表達什麼、轉換時會在哪裡遺失資訊,以及本站哪個工具負責哪個步驟,全程使用同一份五列的聯絡人表格。

每種格式能說什麼、不能說什麼

格式型別巢狀順序註解常見來源
CSV無:每個儲存格都是文字無:只有一張平面表格列與欄否試算表匯出、資料庫
JSON字串、數字、布林值、null物件與陣列,深度不限陣列有序;物件的鍵依慣例否API、指令碼
YAML同 JSON,另加日期同 JSON同 JSON是(#)設定檔
XML無:文字,另加屬性元素,深度不限元素有序是舊系統、電子發票
Markdown 表格無無列與欄否文件、工單

由此可得兩條規則。從表達力較強的格式轉到較弱的格式(JSON 轉 CSV、YAML 轉 Markdown)會遺失型別或結構,唯一的問題是你是否心知肚明地遺失它們。反方向(CSV 轉 JSON)則不會增加資訊:工具無從得知 52000 原本是數字,所以除非有東西去猜,它就會維持字串。

貫穿全文的範例

檔案是 contacts-5.csv:五筆員工紀錄,各有姓名、電子郵件、部門、ISO 格式日期與薪資。

name,email,department,start_date,salary
Ann Lee,ann.lee@example.com,Sales,2021-03-15,52000
Ben Ortiz,ben.ortiz@example.com,Engineering,2019-11-02,88000
Chloe Park,chloe.park@example.com,Sales,2023-06-01,47500
Dev Patel,dev.patel@example.com,Support,2020-01-20,41000
Emma Rossi,emma.rossi@example.com,Engineering,2022-09-12,91000

CSV:引號、逗號與位元組順序記號

CSV 只有一條真正要緊的規則:含有逗號、引號或換行的欄位要用雙引號包起來,欄位內的引號則要寫成兩個。CSV 轉 JSON 在讀取時遵守這條規則。一個內容為 "Prefers calls, not email" 的備註欄被讀成單一值,逗號完整保留;"Says ""remote only""" 讀出來是 Says "remote only";而以空欄位結尾的一列產生的是 "note": "",而不是少了這個鍵。它不做的事是猜型別:輸出中的每個值都是字串,包括 "salary": "52000" 與 "start_date": "2021-03-15"。

從 Excel 另存為「CSV UTF-8」的檔案,開頭會有一個位元組順序記號(BOM),也就是三個看不見的位元組。瀏覽器以文字讀取所選檔案時會丟掉這個記號,所以一份加了 BOM 的聯絡人檔案產生的 JSON 完全相同,第一個鍵是 "name",而不是損壞的鍵。這個記號在反方向才重要:Excel 要靠它才能正確開啟含重音符號的文字,這就是為什麼 HTML 表格轉 CSV 與 Excel 轉 CSV 的單一工作表匯出會寫入它,也是為什麼不寫入 BOM 的 JSON 轉 CSV 所產生的 CSV,在按兩下開啟時可能出現亂碼。

JSON:CSV 從來沒有的型別

JSON 區分 52000 與 "52000",收到後者的指令碼會把薪資按字母順序排序。JSON 格式化是文件送出前檢查的地方。它的「驗證」(Validate)按鈕對一個正確的陣列回報 Valid JSON,對同一個陣列在最後一個物件後多了一個逗號的版本則回報 Error: Unexpected token ']',這是手動編輯時最常見的錯誤。寫成 007 的員工編號以 Unexpected number 失敗,因為 JSON 的數字不能有開頭的零,必須寫成 "007"。而一個十七位數的識別碼 90071992547409931,格式化後變成 90071992547409940:JSON 數字是以雙精度浮點數讀取的,所以很長的數字 ID 也必須是字串。「格式化 JSON」(Format JSON)與「壓縮 JSON」(Minify JSON)在兩個空格縮排的版面與單行之間切換,兩者都不會改變資料。

YAML:同樣的資料,多了日期與意外

JSON 轉 YAML 產生的任何內容都能再讀回來。給它全是字串的聯絡人陣列,它寫出帶引號的 start_date: '2021-03-15' 與 salary: '52000',因為少了引號,YAML 讀取器會把前者變成日期、後者變成數字;給它薪資為 JSON 數字的同一個陣列,它寫出不帶引號的 salary: 52000。引號就是轉換器在告訴你原本的型別是什麼。

讀取手寫的 YAML,才是值會在你不注意時改變的地方。YAML 轉 JSON 拿到沒有引號的 start_date: 2021-03-15,回傳的是 "2021-03-15T00:00:00.000Z",一個 UTC 時間戳記;下一筆紀錄中帶引號的 '2019-11-02' 則維持原樣。badge: 007 變成 7,'012' 維持 "012",salary: 52000 變成數字,而 remote: yes 維持字串 "yes",因為轉換器遵循 YAML 1.2 的規則,只有 true 與 false 才是布林值。凡是必須維持文字的,一律加引號。

XML:元素、屬性與單一子元素陷阱

XML 同樣沒有數字,但它有兩個放值的地方:元素文字與屬性,轉換器需要為兩者訂出慣例。JSON 轉 XML 使用你輸入的根元素名稱,並把名為 @attributes 的鍵當作元素的屬性、#text 當作元素的文字。把前兩筆聯絡人包成 {"contact": [ … ]}、根元素設為 contacts,產生的是一個 <contacts>,內含兩個 <contact> 元素,每個欄位一個子元素;備註裡的 & 與 < 都被跳脫。若改交給它一個裸陣列、根元素設為 contacts,產生的是一個 <contacts>,每筆紀錄成為一個通用的 <item>,文件格式正確,但紀錄失去了名稱;元素名稱重要時,請先把陣列包進物件。有子元素的元素,其子元素一律從新的一行開始;XML 格式化會重新縮排文件,並保留 <?xml?> 宣告。

XML 轉 JSON 把這套慣例反過來:屬性放在 "@attributes" 底下,所有的值都是字串。陷阱在於數量。兩個 <contact> 元素變成一個 "contact" 陣列;一個 <contact> 元素卻變成一個 "contact" 物件,因為轉換器無從得知單一子元素原本應該是清單。一個對 contacts.contact 跑迴圈的指令碼,在兩筆紀錄的檔案上正常運作,在一筆紀錄的檔案上就會失敗。只要資料來源可能只含單一項目,就要檢查它的結構。

把巢狀 JSON 放進平面 CSV

CSV 沒有空間在聯絡人裡放一個地址物件。直接送進 JSON 轉 CSV,一筆含有 "address": {"city": "Taipei"} 的紀錄產生了 [object Object] 這個儲存格,那是轉換器對巢狀值唯一寫得出來的文字。先扁平化:JSON 扁平化把 address.city、address.postcode、phones.0 與 phones.1 變成頂層的鍵,分隔符號可選點或底線,並讓 true 與 null 維持原樣。然後再轉換。

JSON 轉 CSV 以所有物件的鍵的聯集建立標題列,依首次出現的順序排列,物件缺少某個鍵時就讓該儲存格留空:兩筆欄位不同的紀錄產生了標題列 name,department,address,note,phone,第一列的 phone 儲存格是空的,第二列的 address 與 note 是空的。沒有任何警告;信任這個檔案之前,先讀一遍標題列。

Markdown 表格與 HTML 表格

Markdown 表格是資料的圖像,而不是資料。CSV 轉 Markdown 表格把聯絡人檔案寫成實作步驟中所示的管線符號表格,並下載為 table.md;儲存格內的管線符號會跳脫為 \|,其他一律不變。它適合當 Wiki 頁面的終點,不適合當任何還要再被解析的東西的終點。

內部網路頁面上的 HTML 表格,常常是某個數字唯一的出處。HTML 表格轉 CSV 接受貼上的網頁原始碼與一個表格索引(0 代表第一個表格),並以儲存格文字產生 CSV。一個三列的薪資表格回來的是 Ann Lee,Sales,"52,000":網頁中的千分位符號被保留,儲存格也因此加上引號,所以在試算表重新讀取之前,這一欄都是文字。狀態列計為 4 row(s),包含標題列在內。

Excel 的匯入與匯出

Excel 多了 CSV 缺少的兩樣東西:有型別的儲存格,以及不只一個工作表。CSV 轉 Excel 接受一個 CSV 檔案與一個工作表名稱,下載了 contacts-5.xlsx,工作表名為 Contacts;儲存格型別由試算表程式庫依文字判定,和 Excel 開啟 CSV 時的做法差不多,所以事後要檢查員工編號或郵遞區號欄。Excel 轉 CSV 會列出活頁簿的工作表(這裡是 Contacts 與 Notes,回報為 2 個工作表),可將選定的那一個匯出為 Contacts.csv,或把每個工作表匯出為 contacts-csv.zip,內含 Contacts.csv 與 Notes.csv。日期儲存格依 Excel 顯示的樣子寫出:格式設為 YYYY-MM-DD 的到職日期欄輸出為 2021-03-15,而維持地區格式的欄位則會以該格式輸出。

實作:聯絡人轉出再轉回

  1. CSV 轉 JSON。選擇 contacts-5.csv 後產生一個含五個物件的陣列,每個值都是字串。第一個物件:
    {
      "name": "Ann Lee",
      "email": "ann.lee@example.com",
      "department": "Sales",
      "start_date": "2021-03-15",
      "salary": "52000"
    }
  2. CSV 轉 Markdown 表格。同一個檔案產生:
    | name | email | department | start_date | salary |
    | --- | --- | --- | --- | --- |
    | Ann Lee | ann.lee@example.com | Sales | 2021-03-15 | 52000 |
    | Ben Ortiz | ben.ortiz@example.com | Engineering | 2019-11-02 | 88000 |
    | Chloe Park | chloe.park@example.com | Sales | 2023-06-01 | 47500 |
    | Dev Patel | dev.patel@example.com | Support | 2020-01-20 | 41000 |
    | Emma Rossi | emma.rossi@example.com | Engineering | 2022-09-12 | 91000 |
  3. JSON 轉 CSV。把五個物件的陣列貼回去,產生的正是原本的標題列與五行資料;下載的 converted.csv 為 328 位元組,原始檔為 329 位元組,差別在結尾的換行。沒有加上任何引號,因為沒有值需要。
  4. JSON 轉 YAML。同一個陣列變成五個 - name: 區塊,start_date 與 salary 都加了引號,如前文所述。
  5. JSON 轉 XML 再轉回。包成 {"contact": [ … ]}、根元素設為 contacts,在 XML 格式化中整理後,再經 XML 轉 JSON 讀回,五筆紀錄回到一個含五個物件的 contact 陣列,值都是字串,和一開始的結構相同。

這趟來回之所以沒有損失,是因為資料一開始就全是文字。只要加入一個數字、一個巢狀物件或一筆單獨的紀錄,上述陷阱就會出現。

轉換前的檢查清單

  • 決定哪些欄位是數字、日期與 ID,並替 ID 加上引號。
  • 巢狀 JSON 碰到 CSV 之前先扁平化;元素需要真正的名稱時,陣列碰到 XML 之前先包進物件。
  • 手動編輯後驗證 JSON;在 YAML 中替日期與開頭為零的值加上引號。
  • 預期單一 XML 子元素會以物件而非單項陣列的形式回來。
  • 下一個讀取者是 Excel 時,使用會寫入 BOM 的匯出;是指令碼時,使用不帶 BOM 的匯出。