Json.Document Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Json.Document function in Power Query reads JSON text and turns it into values Power Query can work with. A JSON object becomes a record, a JSON array becomes a list, and numbers, text, true/false, and null become the matching M values.

You’ll run into JSON when you pull data from a web API, open a .json file, or get an export where one column is full of JSON text.

In this article, I’ll show you how Json.Document works with practical examples: JSON objects and arrays, nested data, a column full of JSON text, missing keys, and rows where the JSON is broken.

Follow along with the example file

Json.Document Function Examples.xlsx

Download

Json.Document Function Syntax

Json.Document(jsonText as any, optional encoding as nullable number) as any
  • jsonText – the JSON you want to read. This can be text (typed in the formula or taken from a column) or a binary value, such as the file content you get from the File.Contents or Web.Contents function.
  • encoding (optional) – the text encoding of the JSON, such as TextEncoding.Utf16. If you leave it out, Power Query reads the JSON as UTF-8, which is what almost every JSON file and API uses.

What it returns: A record, a list, or a single value, depending on what the JSON holds at the top level. A JSON object {...} gives a record, and a JSON array [...] gives a list.

The Json.Document function is part of the core Power Query M library, so it works wherever Power Query does, including Excel 365, 2024, 2021, 2019, 2016, and Power BI.

To read a JSON file saved on your computer, you pass the file content to it, like Json.Document(File.Contents("C:\Data\orders.json")). The examples below use JSON text instead, so they work in the example file on any computer.

When to Use the Json.Document Function

Use this function when you need to:

  • Read the response from a web API, which almost always comes back as JSON
  • Open a .json file and turn it into a table
  • Parse a column where every cell holds a piece of JSON text
  • Get one value from deep inside a nested JSON structure
  • Clean up JSON data that has missing keys or a few broken rows

Example 1: Convert a JSON Object into a Record

Let’s start with the simplest case. A JSON object is a set of “key”:value pairs inside curly braces, and Json.Document turns it into a Power Query record.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, open the Advanced Editor (Home > Advanced Editor) and use this code:

let
Source = Json.Document("{""name"":""Wayne Cook"",""department"":""Finance"",""age"":34,""remote"":true}")
in
Source

Result: A record with four fields: name (Wayne Cook), department (Finance), age (34), and remote (TRUE)

Json.Document turns the JSON object into a record with name, department, age and remote fields

Here, each key in the JSON becomes a field name, and each value becomes that field’s value. Notice that 34 comes through as a number and true comes through as the logical value TRUE, not as text.

You’ll also notice the doubled quotes. A text value in M sits inside double quotes, so every double quote inside the JSON has to be typed twice (""name"").

Power Query reads each pair as one quote, so the JSON it sees is {"name":"Wayne Cook",...}.

You only need the doubled quotes when you type JSON straight into M code. When the JSON comes from a file, an API, or a worksheet cell, you use it as it is.

In the example file, Examples 1 to 4 and Example 7 have no sheet because they don’t use any worksheet data. You can open them from Data > Queries & Connections.

Example 2: Convert a JSON Array into a List

Now let’s look at the other building block of JSON. A JSON array is a list of values inside square brackets, and Json.Document turns it into a Power Query list.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Json.Document("[""Small"",""Medium"",""Large"",""X-Large""]")
in
Source

Result: A list with four items: Small, Medium, Large, and X-Large

Json.Document turns the JSON array into a list: Small, Medium, Large, X-Large

Here, each item in the JSON array becomes one item in the list, in the same order. If you want it as a table instead, click To Table on the List Tools ribbon, or wrap it in the Table.FromList function.

Example 3: Convert a List of JSON Objects into a Table

This is the shape you’ll see most often. An API or a JSON file usually returns an array of objects, where each object is one row, and you want that as a regular table.

Suppose you have a small staff list in JSON, with a name, hire date, and salary for each person. Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Json.Document("[
{""name"":""Evan Hayes"",""hire_date"":""2024-02-12"",""salary"":68000},
{""name"":""Monica Cooper"",""hire_date"":""2023-09-04"",""salary"":72500},
{""name"":""Cody Brown"",""hire_date"":""2025-01-20"",""salary"":61000},
{""name"":""Molly Perry"",""hire_date"":""2022-11-07"",""salary"":80000}
]"),
#"Converted to Table" = Table.FromRecords(Source),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"name",type text},{"hire_date",type date},{"salary",Int64.Type}})
in
#"Changed Type"

Result: A table with name, hire_date, and salary columns, where the hire dates show as real dates (2/12/2024, 9/4/2023, 1/20/2025, and 11/7/2022)

The JSON array of objects as a table with name, hire_date and salary columns, dates typed as dates

Here, Json.Document turns the JSON array into a list of four records. Then the Table.FromRecords function turns that list into a table, using the keys (name, hire_date, salary) as column names.

The last step sets the data types, and it matters more here than usual.

JSON has no date type, so a date always arrives as text, like “2024-02-12”. Until you change the type, you can’t do date math on that column or use the date filters on it.

The dates display as 2/12/2024 because this computer uses US regional settings. On a computer set to the UK, the same value shows as 12/02/2024.

The ISO format in the JSON (year-month-day) converts the same way on every machine.

If your JSON stores dates in another format, such as “12/02/2024”, use the Date.FromText function instead, because it lets you say which format and culture the text uses.

Example 4: Get a Value from Nested JSON

Real JSON is rarely flat. An order, for example, often holds a customer object and, next to it, a list of items, and you may only need one value from deep inside.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Json.Document("{
""order_id"":""SO-4471"",
""customer"":{""name"":""Nicole Tucker"",""city"":""Boise""},
""items"":[
{""sku"":""LMP-02"",""product"":""Desk Lamp"",""qty"":2},
{""sku"":""CBL-10"",""product"":""HDMI Cable"",""qty"":5},
{""sku"":""MSE-07"",""product"":""Wireless Mouse"",""qty"":1}
]
}"),
City = Source[customer][city]
in
City

Result: Boise

Source[customer][city] returns Boise from the nested customer object

Here, Source is a record, and its customer field holds another record. So Source[customer] gets the customer record, and adding [city] gets the city from inside it.

Lists work the same way, except you use curly braces with a position number that starts at 0. So Source[items]{1}[product] returns HDMI Cable, the product in the second item.

If you’d rather click than type, select the Source step and click on a Record or List link in the preview. Power Query adds a drill-down step that writes this same kind of formula for you.

Important: JSON keys are case-sensitive, and they keep their exact spelling as field names. If the JSON said “City”, then Source[customer][city] would give the error “The field ‘city’ of the record wasn’t found.”

Example 5: Parse a Column of JSON Text into Columns

Here’s a very common situation with exported data. Below is an order list where the Shipping column holds a small piece of JSON for each customer, and I want the city, method, and fee in their own columns.

Orders table with a Customer column and a Shipping column holding JSON text

Create a new blank query, open the Advanced Editor, and use this code. It reads the Orders table by name, so you don’t have to load it into Power Query first.

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
#"Parsed JSON" = Table.TransformColumns(Source,{{"Shipping",Json.Document}}),
#"Expanded Shipping" = Table.ExpandRecordColumn(#"Parsed JSON","Shipping",{"city","method","fee"},{"City","Method","Fee"})
in
#"Expanded Shipping"

Result: The Shipping column is replaced by City, Method, and Fee columns (for example, Austin, Standard, and 4.99 for Veronica King)

The Shipping JSON expanded into City, Method and Fee columns

Here, the Table.TransformColumns function runs Json.Document on every cell of the Shipping column. Each piece of JSON text becomes a record, so the column now holds one record per row.

Then the Table.ExpandRecordColumn function spreads those records out into columns. The first list names the JSON keys to pull out, and the second list gives the new columns nicer names (City instead of city).

The new columns don’t have a data type yet, so their headers show the ABC123 (any) icon. It’s good practice to add a type step like the one in Example 3, so Fee is set as a number.

You can also do this without writing code. Select the column, go to Transform > Parse > JSON, and then click the expand icon in the column header.

Power Query writes similar steps for you. By default, the expand box adds the original column name as a prefix (Shipping.city), so uncheck that option if you want plain names.

Example 6: Expand a JSON Array into Separate Rows

Sometimes each cell holds a JSON array instead of an object. Below is a list of support tickets where the Tags column holds the tags for each ticket as a JSON array, and I want one row per tag.

Tickets table with a Tags column holding a JSON array per ticket, including an empty array

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
#"Parsed JSON" = Table.TransformColumns(Source,{{"Tags",Json.Document}}),
#"Expanded Tags" = Table.ExpandListColumn(#"Parsed JSON","Tags")
in
#"Expanded Tags"

Result: Seven rows: six rows for the six tags (TK-303 gets three) and one row for TK-304 with null in the Tags column

One row per tag, with null for TK-304's empty array

Here, Json.Document turns each cell into a list, because each one holds a JSON array. Then the Table.ExpandListColumn function gives every item in a list its own row and repeats the Ticket ID next to it.

TK-304 has an empty array ([]), so it has no tags to expand. Power Query still keeps the ticket as one row with null in the Tags column, so no ticket quietly disappears from your data.

Example 7: Handle JSON Objects with Missing Keys

Here’s a problem you’ll hit with real-world JSON. Not every object always has the same keys, and some keys may hold null.

Suppose you have a list of contacts where Gordon Robinson has no phone key at all, and Scott Wilson has a phone key set to null. Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Json.Document("[
{""name"":""Arthur Porter"",""email"":""arthur.p@example.com"",""phone"":""555-0142""},
{""name"":""Gordon Robinson"",""email"":""g.robinson@example.com""},
{""name"":""Scott Wilson"",""email"":""scott.w@example.com"",""phone"":null},
{""name"":""Carrie Sullivan"",""email"":""carrie.s@example.com"",""phone"":""555-0198""}
]"),
Contacts = Table.FromRecords(Source,{"name","email","phone"},MissingField.UseNull)
in
Contacts

Result: A table with name, email, and phone columns, where the phone is null for both Gordon Robinson and Scott Wilson

Contacts table with null phone for Gordon Robinson (missing key) and Scott Wilson (JSON null)

Here, the JSON null in Scott Wilson’s record becomes a regular M null, so that one needs no special handling. Gordon Robinson’s record is the problem, because it has no phone field at all.

If you use Table.FromRecords(Source) on its own, the query fails with “The field ‘phone’ of the record wasn’t found.” The extra arguments fix that: the list names the columns you want, and MissingField.UseNull fills any missing field with null instead of raising an error.

Keep in mind that the table only gets the columns you list. If the JSON later gains a new key, such as a job title, it won’t show up until you add it to the list.

Example 8: Handle Rows That Have Invalid JSON

Sometimes a few cells hold JSON that’s cut off or badly typed. Below is a log of thermostat readings where the Payload column holds JSON, one of the rows (R-103) got cut off, and I want the device, temperature, and humidity in their own columns.

Readings table with a Payload JSON column where R-103 is cut off

If you use Json.Document directly on this column, R-103 can’t be parsed, so its cell shows Error instead of a record. Click it and you’ll see the message “We reached the end of the buffer.”

That error carries into the Device, Temp, and Humidity cells of R-103 after you expand the column. To get a clean null instead, wrap Json.Document in try ... otherwise. Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
#"Parsed JSON" = Table.TransformColumns(Source,{{"Payload",each try Json.Document(_) otherwise null}}),
#"Expanded Payload" = Table.ExpandRecordColumn(#"Parsed JSON","Payload",{"device","temp","humidity"},{"Device","Temp","Humidity"})
in
#"Expanded Payload"

Result: Device, Temp, and Humidity columns for R-101, R-102, and R-104, and null in all three columns for R-103

Device, Temp and Humidity columns, with null for the invalid R-103 row

Here, try attempts to parse each cell. When the JSON is valid, you get the record as usual, and when it isn’t, otherwise null puts a null there instead of an error.

R-103 stays in the table with empty values, so you can still see which reading failed and go back to fix it at the source. If you’d rather drop those rows, filter out the nulls in the Device column.

Note that this step replaces the original Payload text, so the query no longer shows what was wrong with R-103. To see the broken text, look at the source table in the worksheet.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.