Json.FromValue

Accessing Data

Serializes an M value to JSON and returns it as binary.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Json.FromValue(value as any, optional encoding as nullable number) as binary

Parameters

NameTypeRequiredDescription
valueanyYesThe M value to serialize. Can be a record, list, table, text, number, logical, null, date, datetime, or duration.
encodingnumberNoThe text encoding to use. Use TextEncoding.Utf8 (65001) for UTF-8 (default), TextEncoding.Utf16 (1200) for UTF-16.

Return Value

binaryThe JSON representation of the input value as binary data, encoded using the specified encoding (default UTF-8).

Remarks

Json.FromValue serializes an M value to its JSON representation and returns the result as a binary value encoded in UTF-8 (or a specified encoding). This is the inverse of Json.Document, which parses JSON text or binary into M values. The primary use case is constructing JSON request bodies for REST API calls via Web.Contents.

Supported M types and their JSON equivalents: - record → JSON object ({}) - list → JSON array ([]) - table → JSON array of objects (each row becomes an object, column names become keys) - text → JSON string - number → JSON number (integer or decimal) - logical → JSON boolean (true / false) - null → JSON null - date, datetime, datetimezone → JSON string in ISO 8601 format - duration → JSON string in ISO 8601 duration format

Result type is binary: The function returns binary, not text. When you need readable JSON text (for debugging or display), wrap the call with Text.FromBinary. When using the result as an HTTP request body with Web.Contents, pass it directly to the Content option — Web.Contents accepts binary for the body.

Encoding parameter: The default encoding is UTF-8 (TextEncoding.Utf8, code page 65001). Pass TextEncoding.Utf16 (1200) as the second argument if the target system requires UTF-16 encoding.

Unsupported types: binary values, function values, and type values cannot be serialized and will raise an error. If your data contains binary columns in a table, remove or transform them before calling Json.FromValue.

Roundtrip with Json.Document: You can round-trip a value through JSON using Json.Document(Json.FromValue(value)), though some M types (such as date without time component) may deserialize as text rather than the original type.

Examples

Example 1: Serialize a record to JSON binary

```powerquery

Json.FromValue([Name = "Alice", Age = 30, Active = true])
// Returns binary: {"Name":"Alice","Age":30,"Active":true}

Example 2: Convert to readable JSON text with Text.FromBinary

```powerquery

Text.FromBinary(Json.FromValue([City = "London", Country = "UK"]))
// Returns: {"City":"London","Country":"UK"}

Example 3: Serialize a list to a JSON array

```powerquery

Text.FromBinary(Json.FromValue({1, 2, 3, "four", true, null}))
// Returns: [1,2,3,"four",true,null]

Example 4: Build a JSON request body and POST to a REST API

```powerquery

let
    RequestBody = Json.FromValue([
        reportType = "Sales",
        startDate  = "2024-01-01",
        endDate    = "2024-12-31",
        filters    = [region = "North America"]
    ]),
    Response = Web.Contents(
        "https://api.example.com/reports",
        [
            Headers = [#"Content-Type" = "application/json"],
            Content  = RequestBody
        ]
    ),
    Result = Json.Document(Response)
in
    Result

Example 5: Serialize a table to a JSON array of objects

```powerquery

let
    Data = #table(
        {"Product", "Sales"},
        {{"Widget A", 1500}, {"Widget B", 2300}}
    ),
    JsonText = Text.FromBinary(Json.FromValue(Data))
in
    JsonText
// Returns: [{"Product":"Widget A","Sales":1500},{"Product":"Widget B","Sales":2300}]

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks