---
title: Json.Document – แปลง JSON Text เป็น Power Query Record
url: https://www.thepexcel.com/functions/power-query/accessing-data-functions/json-document/
type: function-explainer
program: Power Query
syntax: "= Json.Document(jsonText, optional encoding)"
date: 2025-12-12
updated: 2025-12-20
scores:
  popularity: 8
  difficulty: 3
  usefulness: 9
---

# Json.Document – แปลง JSON Text เป็น Power Query Record

> Json.Document ใช้สำหรับแปลง JSON text หรือ binary data ให้เป็น Power Query record ทำให้คุณสามารถเข้า

## คำอธิบาย

Json.Document ใช้สำหรับแปลง JSON text หรือ binary data ให้เป็น Power Query record ทำให้คุณสามารถเข้าถึง field แต่ละตัวได้อย่างง่ายดาย

## Syntax

```excel
= Json.Document(jsonText, optional encoding)
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| jsonText | Yes | Text หรือ Binary |  | JSON content ที่ต้องการแปลง อาจเป็น text string หรือ binary data จาก File.Contents หรือ Web.Contents |
| encoding | No | TextEncoding.Type | TextEncoding.Utf8 | Encoding ของ JSON document (เช่น TextEncoding.Utf8, TextEncoding.Utf16) ถ้าไม่ระบุ Power Query จะใช้ UTF-8 โดยอัตโนมัติ |

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน: แปลง JSON Text เป็น Record

```excel
let
    JsonText = "{\"name\": \"สมชาย\", \"age\": 28, \"city\": \"กรุงเทพ\"}",
    Parsed = Json.Document(JsonText)
in
    Parsed
```

**ผลลัพธ์:** `Record: [name = "สมชาย", age = 28, city = "กรุงเทพ"]`

แปลง JSON string ให้เป็น Power Query record ที่สามารถเข้าถึงแต่ละ field ได้ (เช่น Parsed[name], Parsed[age])

### 2. แปลง JSON Array เป็น List

```excel
let
    JsonArray = "[{\"id\": 1, \"product\": \"Laptop\"}, {\"id\": 2, \"product\": \"Mouse\"}]",
    ParsedList = Json.Document(JsonArray),
    ConvertedTable = Table.FromList(ParsedList, Splitter.SplitByNothing(), {\"Data\"})
in
    ConvertedTable
```

**ผลลัพธ์:** `ตาราง 2 แถว แต่ละแถวเป็น record ที่มี field id และ product`

แปลง JSON array ให้เป็น list ของ record จากนั้นใช้ Table.FromList เพื่อแปลงเป็นตาราง

### 3. อ่าน JSON จากไฟล์ local

```excel
let
    JsonFile = File.Contents("C:\\Data\\Products.json"),
    Parsed = Json.Document(JsonFile)
in
    Parsed
```

**ผลลัพธ์:** `Record หรือ List ที่มาจากไฟล์ Products.json`

อ่านไฟล์ JSON จาก disk โดยใช้ File.Contents แล้วแปลง binary data เป็น Power Query record

### 4. เรียก JSON จาก Web API

```excel
let
    ApiUrl = "https://api.example.com/users",
    Response = Web.Contents(ApiUrl, [Headers=[#\"Authorization\"=\"Bearer token123\"]]),
    Parsed = Json.Document(Response),
    UserTable = Table.FromList(Parsed, Splitter.SplitByNothing(), {\"User\"})
in
    UserTable
```

**ผลลัพธ์:** `ตารางของ user records จาก API`

เรียก REST API ที่ return JSON ก่อน จากนั้นใช้ Json.Document แปลง JSON response เป็น Power Query record

### 5. ระบุ Encoding สำหรับ JSON ที่เป็น UTF-16

```excel
let
    JsonBinary = File.Contents("C:\\Data\\Unicode.json"),
    Parsed = Json.Document(JsonBinary, TextEncoding.Utf16)
in
    Parsed
```

**ผลลัพธ์:** `Record ที่ parsed จาก JSON file ที่เข้ารหัสเป็น UTF-16`

กำหนด encoding อย่างชัดเจนเมื่อ JSON file ใช้ encoding ที่ไม่ใช่ UTF-8 เช่น UTF-16

## หมายเหตุเพิ่มเติม

- ใช้ Json.Document กับ Web.Contents เมื่อ call API ทำให้ code สั้นลง: Json.Document(Web.Contents(url))

- ถ้า JSON complex และ deeply nested ให้ expand ทีละขั้น ไม่ต้องมึนงง

- Test JSON string ใน online JSON validator (เช่น jsonlint.com) ก่อนใช้กับ Json.Document

- Json.Document ทำงานใน Power Query, Power BI, Excel ทุกที่ 100% compatible

- ถ้า API return JSON array (ไม่ใช่ object) คุณจะได้ list แล้วต้องใช้ Table.FromList เพื่อแปลงเป็นตาราง

## คำถามที่พบบ่อย

**Q: Json.Document ต่างจาก Json.FromValue ยังไง?**

Json.Document คือการแปลง JSON text/binary เป็น record (parsing) ส่วน Json.FromValue คือการแปลง Power Query record/list เป็น JSON text (serializing) ตรงกันข้ามเลย

**Q: ถ้า JSON text มี error format จะเกิดอะไร?**

Power Query จะ throw error แล้วบอกว่าตำแหน่งไหนใน JSON ที่มี syntax error เช่น missing quote หรือ comma ที่ไม่ครบ ต้องแก้ JSON format ให้ถูกต้องก่อน

**Q: ใช้ Json.Document กับ API ที่ return nested JSON ได้ไหม?**

ได้เลย Json.Document จะ parse nested objects และ arrays ให้เป็น nested records และ lists ในภายหลังคุณสามารถใช้ record access syntax หรือ Table.ExpandRecordColumn เพื่อ expand nested data

**Q: Json.Document ต้องใช้ with Web.Contents หรือ File.Contents เสมอไหม?**

ไม่จำเป็น Json.Document สามารถรับ JSON text string โดยตรง หรือ binary data ก็ได้ ใช้กับ Web.Contents หรือ File.Contents เพราะว่ามันสะดวก แต่ถ้า JSON text อยู่ในคอลัมน์ของตาราง ก็สามารถ parse ได้เช่นเดียวกัน

## ฟังก์ชันที่เกี่ยวข้อง

- [Json.FromValue – แปลง Value เป็น JSON](https://www.thepexcel.com/functions/power-query/text-functions/json-fromvalue/)
- [Web.Contents – ดึงข้อมูลจากเว็บและ REST API](https://www.thepexcel.com/functions/power-query/accessing-data-functions/web-contents/)
- [File.Contents – อ่านข้อมูลจากไฟล์](https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/)
- [Table.ExpandRecordColumn – ขยายคอลัมน์ที่เป็น Record ให้เป็นหลายคอลัมน์](https://www.thepexcel.com/?post_type=function-explainer&p=37949)

## แหล่งข้อมูลเพิ่มเติม

- [Json.Document - Microsoft Learn](https://learn.microsoft.com/en-us/powerquery-m/json-document) _(official)_
- [Parse text as JSON or XML - Power Query](https://learn.microsoft.com/en-us/power-query/parse-json-xml) _(official)_
- [Chris Webb's BI Blog: Generating JSON In Power BI And Power Query](https://blog.crossjoin.co.uk/2016/10/07/generating-json-in-power-bi-and-power-query/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/accessing-data-functions/json-document/](https://www.thepexcel.com/functions/power-query/accessing-data-functions/json-document/)_
