Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Json.SchemaFromType: How to Use It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Json.SchemaFromType is a Power Query M function that takes a table type and returns a JSON schema text describing that type's structure. You use it when you need a schema document for an API, a validation step, or documentation — not to convert data. It does not read your data; it only reads the type definition you give it. The shortest working recipe: open a blank query in the Power Query Editor, enter in the formula bar, and press Enter. The result is a text value containing a JSON schema. Load it to a cell or a file if you need to keep it.

What You'll Need

  • Power Query available in Excel, Power BI Desktop, or another host that exposes the full M engine. The function is part of the standard library, so no add-in is required.
  • A clear idea of the table type you want to describe: column names and M data types such as text, number, date, datetime, logical, or any.
  • Access to the Power Query Editor (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI Desktop: Home > Transform data). Menu labels can differ slightly by version, device, and region.
  • For output you want to keep: a destination cell range or a file path, depending on your host.

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, use Home > Transform data. The exact wording of the ribbon group can vary between versions.
  2. Create a blank query. In the Home tab, choose New Source > Other Sources > Blank Query. A new query appears in the Queries pane with a single step in Applied Steps.
  3. Open the formula bar. If it is hidden, enable it from the View tab. You need the formula bar because the function is entered as an expression, not through a dialog.
  4. Enter the function call. Replace the contents of the formula bar with a call that supplies a table type. For example: = Json.SchemaFromType(type table [CustomerID = text, OrderDate = date, Total = number]) Press Enter. The query preview should now show a single text value — the JSON schema.
  5. Check the schema text. Click into the preview cell to confirm the schema contains the properties you expect. The function maps M types to JSON schema types; for example, text becomes a string, number becomes a number, and logical becomes a boolean. Date and datetime types are represented in a way that preserves the type information, so verify the output matches what your downstream consumer expects.
  6. Name the query. In the Query Settings pane, rename the query to something meaningful, such as OrderSchema. This makes it easier to reference from other queries.
  7. Use or export the result. If you only need the schema text, right-click the value in the preview and copy it. To keep it in the workbook, close and load the query to a table; the single text value lands in a cell. If you need a file, add a step that writes the text using the host's file functions, or copy the text into a text editor and save it with a .json extension.
  8. Reuse the type elsewhere. If you already have a query whose output has the right shape, you can reference its type instead of retyping it. For example, if a query named Orders returns a table, use = Json.SchemaFromType(type Table.FromRows({}, {"CustomerID", "OrderDate", "Total"})) only when you have defined the types explicitly; otherwise build the type record directly as shown in step 4. The function needs a type value, not a table value.

Common Mistakes

  • Passing a table instead of a type. Json.SchemaFromType(Orders) fails because Orders is a table value. You must pass a type, such as or a named type.
  • Forgetting the type keyword. Writing without type in front produces a table value, not a type value, and the function rejects it.
  • Using unsupported type names. M types are specific: text, number, logical, date, datetime, time, duration, any. Guessing names like string or integer causes an error.
  • Expecting data conversion. The function does not validate rows or convert a table to JSON. It only emits a schema describing a type. To convert data, use Json.FromValue instead.
  • Losing the text on load. If you load the query to a table, the schema appears as a single cell of text. If you need it as a file, export it deliberately rather than assuming the load step writes a .json file.
  • Assuming the schema is complete for every consumer. Different JSON schema tools expect different keywords. The output is a schema, but you may need to add metadata such as titles or required fields by editing the text afterward.

Tips

  • Define the type once in a separate query or a let expression and reference it, so the schema and any related transformations stay in sync.
  • Use type table with explicit column types rather than relying on any; a schema full of any is less useful for validation.
  • If you need a schema for a nested structure, build the type with nested records or tables, then pass it to the function. The output reflects the nesting.
  • Keep the schema query separate from your data queries. It has no rows, so loading it alongside data can confuse report consumers.
  • When sharing the schema, paste it into a JSON formatter to check it is valid before handing it to another system.

Troubleshooting

If the formula bar shows an error immediately after pressing Enter, check the type expression first: confirm the word type is present, every column has a valid M type, and brackets and commas are balanced. If the query loads but the preview shows an error value, click the error to read the message; it usually names the argument that is wrong. If the function is not recognized at all, confirm you are in a host that exposes the full M library and that the query is not set to a compatibility level that hides newer functions. If the schema text looks wrong, compare each column's M type against the JSON schema type you expected and adjust the type definition. If you cannot find the formula bar, enable it from the View tab; if the ribbon labels differ from these steps, look for the equivalent group in your version. When in doubt, start a fresh blank query and enter the simplest possible call, = Json.SchemaFromType(type table [A = text]), to confirm the function works before adding complexity.

Frequently Asked Questions

What does Json.SchemaFromType actually return?

It returns a text value containing a JSON schema that describes the table type you supplied. It does not return a table, and it does not read any data rows.

Can I use it on an existing query's output?

Only if you can supply that query's type. The function takes a type value, not a table value, so you either write the type explicitly or reference a named type. Passing a query name that returns a table will fail.

Does it work in Excel and Power BI Desktop?

Yes, it is part of the standard M library available in both. The editor and ribbon labels differ slightly between hosts and versions, but the function call is the same.

Can I convert a table to JSON with this function?

No. Use Json.FromValue to serialize data. Json.SchemaFromType only produces a schema describing a type.

Why does my schema show "any" for some columns?

Because the type definition used any for those columns. Replace any with the specific M type, such as text or number, and regenerate the schema.

Can I edit the schema after generating it?

Yes. The output is plain text, so you can copy it and modify it in any text editor. Changes you make are not reflected back in the query unless you edit the query steps.