OpenBI Docs Buy

TOJSON

A table's first rows (10 by default) as JSON text: its header, row count and data.

Group: Table manipulation. Compatible with DAX.

Syntax

TOJSON ( <Table> [, <MaxRows>] )
ParameterTypeDescription
TableTableThe rows to write.
MaxRowsNumber
optional
How many rows to write: 10 when left out, any negative number for all of them.

Returns

Text. A JSON object with the columns' names (header), the table's number of rows (rowCount) and the rows written (data).

Remarks

  • rowCount is the number of rows of the whole table, even when fewer are written.
  • Text is written in quotes, numbers with a decimal point and at most four decimals, dates in quotes as 2026-01-31T14:30:00, TRUE and FALSE as true and false, and a blank as null.
  • Column names are written as in a query: 'Holdings'[Bond] for a column of the model, [Name] for one made in the query.
  • With no row to write, the result is BLANK.
  • Rows are written in the order the table has them. It is meant for looking at a table while you write a measure.

Examples

A small table as JSON

The blank liters are null; 64 is written 64.0.

EVALUATE
VAR Orders =
    DATATABLE (
        "Product", STRING, "Liters", INTEGER, "Price", DOUBLE,
        { { "Pils", 120, 38 }, { "IPA", 80, 64 }, { "Cider", BLANK (), 52 }, { "Stout", 45, 70 } }
    )
RETURN
    ROW ( "JSON", TOJSON ( Orders ) )
JSON
{ "header": ["[Product]", "[Liters]", "[Price]"], "rowCount": 4, "data": [ ["Pils", 120, 38.0], ["IPA", 80, 64.0], ["Cider", null, 52.0], ["Stout", 45, 70.0] ] }

The text has line breaks and tabs, which the result table shows as spaces.

Two rows of five

rowCount says 5, the table's size, though only two rows are written.

EVALUATE
ROW (
    "JSON",
        TOJSON (
            SELECTCOLUMNS ( Holdings, "Bond", Holdings[Bond], "Maturity", Holdings[Maturity], "Price", Holdings[Price] ),
            2
        )
)
JSON
{ "header": ["[Bond]", "[Maturity]", "[Price]"], "rowCount": 5, "data": [ ["Government 2028", "2028-03-02T00:00:00", 97.84], ["Government 2033", "2033-03-15T00:00:00", 92.15] ] }

The text has line breaks and tabs, which the result table shows as spaces.

TOCSV, CONCATENATEX, EVALUATEANDLOG, SELECTCOLUMNS.

Last updated · OpenBI Studio 0.3.74