Dax Function: TOJSON

Category: Other Functions

The TOJSON function in Power BI is used to convert table data into a JSON-formatted string. It is particularly useful for integrating Power BI with external applications or services that utilize JSON for data exchange.

Purpose

  • Data Integration: Allows data to be shared with JSON-compatible systems.

  • Debugging: Provides a textual representation of data in JSON format.

  • Custom Applications: Useful for embedding Power BI data in applications requiring JSON input.

Type of Calculations

  • Performs textual transformation of table data into a JSON string.

  • Does not modify or aggregate the data, focusing solely on converting the format.

Practical Use Cases

  1. APIs and Web Services: Send Power BI data as JSON payloads to REST APIs.

  2. Custom Reporting: Embed JSON-formatted data into web-based reports or dashboards.

  3. Debugging: Visualize table data as JSON for troubleshooting.


TOJSON(table)

ParameterTypeDescription
tableTableThe input table to be converted into a JSON string.

How Does TOJSON Dax Works

  • Input: Accepts a table containing rows and columns.

  • Transformation: Converts each row into a JSON object, with column names as keys and cell values as corresponding values.

  • Output: Returns a single string representing the table as an array of JSON objects.

What Does It Return?

  • Type: Text

  • Description: A string representing the table data in JSON format, including keys and values.

When Should We Use It?

  • When exporting data to systems that require JSON format.

  • For creating API integrations or sharing structured data.

  • When debugging or inspecting data structure.

Examples

Basic Usage :

Convert a simple table to JSON:


TOJSON(VALUES(Sales))

[
{"Product": "Product1", "Amount": 100},
{"Product": "Product2", "Amount": 200}
]"Product1,100,200\nProduct2,300,400"

Column Usage

Export a specific column to JSON:


TOJSON(SELECTCOLUMNS(Sales, "Product", Sales[Product]))

[
{"Product": "Product1"},
{"Product": "Product2"}
]"Product1\nProduct2\nProduct3"

Advanced Usage

Filter and export table data:


FilteredJSON =
TOJSON(
FILTER(Sales, Sales[Amount] > 500)
)

[
{"Product": "Product3", "Amount": 600},
{"Product": "Product4", "Amount": 700}
]"Product2;600\nProduct3;700"

Tips and Tricks

  • Validate JSON: Use online validators or parsers to ensure the generated JSON is well-formed.

  • Optimize Table Structure: Ensure table data is clean and flattened for more readable JSON output.

  • Combine with DAX Filters: Extract specific data subsets for targeted JSON export.

Performance Impact of TOJSON DAX Function:

  • Memory Usage: Converting large tables into JSON may consume significant resources.

  • Optimization: Filter or select specific columns to minimize the size of the output.

Related Functions You Might Need

  • CONCATENATEX: Combine data into a single string, useful for custom JSON formatting.

  • SELECTCOLUMNS: Extract specific columns before converting to JSON.

  • FILTER: Narrow down table data for more focused JSON output.

Want to Learn More?
For more information, check out the official Microsoft documentation for TOJSON You can also experiment with this function in your Power BI reports to explore its capabilities.

Unlock the full capabilities of Power BI and elevate your data insights with our specialized consulting services. Whether you need guidance on advanced DAX functions like those highlighted here, support in designing interactive dashboards, or expertise in optimizing data models for enhanced performance, our experienced Power BI consultants are equipped to deliver customized solutions for your business. Explore our Power BI Consulting Services page to discover how we can help your organization make smarter, data-driven decisions.

1. What does the TOJSON function do in Power BI?

Converts table data into a JSON-formatted string for use in APIs or external applications.

2. Can I use TOJSON to create nested JSON objects?

No, TOJSON produces flat JSON structures. Use DAX expressions to manipulate the table before conversion.

3. Is TOJSON suitable for large datasets?

While it works with large datasets, performance may be impacted. Use filters to optimize the output.

4. What tools can I use to test JSON output?

JSON validators like jsonlint.com or built-in IDE tools can validate the JSON.

5. Can I specify the JSON keys?

Yes, by using SELECTCOLUMNS, you can control which columns become keys in the JSON output.