Skip to end of banner
Go to start of banner

Using JSONata to configure Custom Reports

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Overview

JSONata is a powerful data query and transformation language designed for processing JSON data. It is used to preprocess, merge, and format data retrieved from APIs, allowing developers to perform complex calculations and data manipulations.

Key Features of JSONata

  1. Data Transformation: Modify and enhance JSON data by adding calculated fields.

  2. Merging Data: Combine information from multiple JSON data sources.

  3. Advanced Query Functions: Perform calculations using functions like map, filter, distinct, and sort.

  4. Debugging Support: Explore and debug the data processing context using debug tools.

  5. Data Visualization Integration: Use transformed JSON data to create dynamic visualizations.

Step-by-Step Guide

Example: Processing API Data

In this example, we will process data retrieved from an API, calculate additional attributes (like the birth decade), and generate a visual chart.

Step 1: Retrieve API Data

  1. Use a REST node to fetch data from an API endpoint. For example:

    {
      type: 'rest',
      url: 'https://randomuser.me/api/?results=3
    
      children:  [
          {type: 'json'}
       ]
    }

    This will generate data for 3 people.

  2. Inspect the initial data structure.


Debugging and Context Exploration

Enabling Debug Mode

  1. Toggle debug mode to view additional buttons for inspecting the report context.

  2. Use the Inspect option in your browser developer tools to access the console.

  3. Explore the JSON context at various levels:

    • Root context: Contains global data sources and metadata.

    • Node context: Contains data specific to the current processing node.


Tips and Best Practices

  1. Start Small: Begin with simple queries to understand the structure of your data.

  2. Leverage Debug Mode: Use the debug tools to trace errors and verify transformations.

  3. Utilize Functions: JSONata provides a wide range of built-in functions for advanced data manipulation.

  4. Reuse and Optimize: Store reusable queries in variables to simplify and optimize transformations.


Common Functions in JSONata

Function

Description

Example

$map

Maps a function to each element in an array.

$map(data, $p -> $p.name)

$filter

Filters elements based on a condition.

$filter(data, $p -> $p.age > 20)

$merge

Merges objects into a single object.

$merge([$a, $b])

$distinct

Returns unique values from an array.

$distinct(data)

$sort

Sorts an array.

$sort(data)

$count

Counts elements in an array.

$count(data)

  • No labels