The Json.Document function in Power Query is used to convert a JSON-formatted string or a binary file into a structured Power Query object, such as a record or a list. To use it, you typically wrap the source of your data (like a file or a web API response) within the function, which allows Power Query to interpret the curly braces and brackets of JSON as rows and columns rather than plain text. This is the fundamental step for anyone importing data from REST APIs or configuration files into Excel or Power BI.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A source of JSON data, which could be a local
.jsonfile, a URL from a web API, or a text string stored in a cell. - Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Import the Raw Data: Start by bringing your data into the Power Query Editor. If you are using a file, go to the Data tab, select Get Data > From File > From JSON. If you are using a web source, select Get Data > From Other Sources > From Web. Note that labels may differ slightly depending on your version of Excel or Power BI.
- Identify the Binary Source: When you import a JSON file via the standard UI, Power Query often applies
Json.Documentautomatically. However, if you are writing a custom query or dealing with a column of JSON strings, you will see the data as "Binary" or "Text". To manually apply the function, click the fx button next to the formula bar to create a new step. - Apply the Function: In the formula bar, wrap your previous step name with the function. For example, if your previous step was named
Source, change the formula to= Json.Document(Source). If the data is in a specific column of a table, you would go to Add Column > Custom Column and enter= Json.Document([ColumnName]). - Convert the Result to a Table: Once
Json.Documentis applied, the data usually appears as a "Record" or a "List". If it is a List, click the To Table button in the Transform tab. This converts the list of JSON objects into a table format that Power Query can expand. - Expand the Records: You will now see a column containing "Record" values. Click the small expand icon (two diverging arrows) in the top-right corner of the column header. A list of all available fields within the JSON object will appear. Uncheck "Use original column name as prefix" to keep your headers clean, then click OK.
- Define Data Types: JSON does not inherently store strict data types like SQL does. After expanding your columns, select the columns and use the Transform tab to set the correct data type (e.g., Decimal Number, Date, or Text) to ensure your reports calculate correctly.
- Load the Data: Once the JSON is fully flattened into a table, click Close & Apply (Power BI) or Close & Load (Excel) to bring the structured data into your worksheet or data model.
Common Mistakes
One of the most frequent errors is attempting to use Json.Document on data that is already parsed. If you use the "From JSON" connector, Power Query applies the function in the first step. Adding it again will result in an error because the function expects a binary or text input, not a Power Query record. Always check the formula bar to see if Json.Document is already present before adding it manually.
Another common mistake is neglecting the "To Table" step. When a JSON file contains an array (starting with [), Json.Document returns a List. Many users try to expand the list immediately, but you must first convert that list into a table. Without this conversion, you cannot access the column expansion tool, leading users to believe the data is missing or corrupted.
Users also often forget to handle nested JSON. JSON is hierarchical, meaning a record can contain another record or another list. A single expansion step only goes one level deep. If your data has multiple levels of nesting, you will see "Record" or "List" values again after the first expansion. You must repeat the expansion process for every nested level until all desired data is visible as primitive values.
Tips
To handle dynamic JSON keys, avoid relying solely on the GUI expansion tool. The GUI hard-codes the column names into the M code (e.g., Columns = {"ID", "Name", "Date"}). If the API provider adds a new field or changes a key name, your query will fail or skip the new data. To solve this, you can use Record.FieldNames to dynamically retrieve the keys of the JSON record before expanding.
When dealing with very large JSON files, performance can degrade during the expansion phase. To optimize this, filter your rows as early as possible. If you only need a subset of the data, try to filter the list before converting it to a table and expanding the records. This reduces the memory overhead required to maintain the expanded table in the Power Query cache.
If you are importing JSON from a web API that requires authentication, use the Web.Contents function in conjunction with Json.Document. This allows you to pass headers or API keys in the options record of Web.Contents, ensuring that the binary data passed to Json.Document is the actual response body and not an HTML error page.
Troubleshooting
If you encounter a "We cannot convert the value to type Binary" error, it means the input provided to Json.Document is not in a format the function recognizes. Check if you are passing a table instead of a single cell value. If your JSON is in a table column, you must use a Custom Column or a function like Table.TransformColumns to apply Json.Document to each row individually.
If the result of the function is a single record but you expected a list (or vice versa), check the structure of your source JSON. A JSON file starting with { is a single object (Record), while one starting with [ is a collection (List). If the API returns a wrapper object (e.g., {"data": [ ... ]}), you must first click on the "data" record to drill down into the list before you can convert it to a table.
When you see "Expression.Error: The input argument to Json.Document is not a valid JSON string," the source data is likely not valid JSON. This often happens when a web API returns an HTML error page (like a 404 or 500 error) instead of JSON. To fix this, check the source URL in a browser or a tool like Postman to ensure the endpoint is active and returning the expected format.
If the expansion step is missing columns that you know exist in the JSON, it is usually because the first few rows of the data do not contain those fields. Power Query samples the first 1,000 rows to determine the columns. If the new fields only appear in row 2,000, they won't show up in the expansion menu. You can manually edit the M code in the formula bar to add the missing column names to the list of fields to expand.
Frequently Asked Questions
Can Json.Document handle XML data?
No, Json.Document is specifically designed for JSON. For XML files, you must use Xml.Tables or Xml.Document to parse the data correctly.
Do I need to install a separate driver to use this function?
No, Json.Document is a built-in function of the Power Query (M) language and is available by default in Excel and Power BI.
What happens if the JSON structure changes in the source file?
If you used the standard GUI expansion, the query may error or ignore new columns. Using dynamic column retrieval via Record.FieldNames can prevent this.
Is there a limit to the size of the JSON file I can parse?
There is no hard-coded limit, but very large files can consume significant RAM. Using Table.Buffer or filtering data early can help with performance.
Why does my JSON data look like a single cell saying "Record"?
This is normal behavior. Json.Document converts the text into a Power Query Record object. You must click the expand icon in the column header to see the actual values.
Can I use Json.Document with a local text file?
Yes, as long as the text file contains valid JSON formatting. You can import it as a text file and then wrap the content in the Json.Document function.