Prepare JSON

General information

Brick converts input data into a dictionary followed by JSON serialization format.

Description

Brick Locations

Bricks Data Manipulation → Prepare JSON

Brick Parameters

  • Orient
    • Indication of the JSON format we expect to convert to. This field is obligatory for any setting selected. Users can choose one of the presented formats:
      1. dict Standard Python format: {key: {index1: value1, index2: value2, …}, …}, where key indicates a data frame column, and the value is a dictionary, constructed as ‘index: value’ pairs.
      1. list
        1. Standard Python format: {key: [value1, value2, value3, …], …}, where key indicates a data frame column, and the value is the list of corresponding column values.
      1. split
        1. Dictionary in such format → {”index”: [index1, index2, …], “columns”: [column_name1, column_name2, …], ‘”data”: [[col1_value1, col2_value1, …], [col1_value2, col2_value2, …], …]}
      1. records
        1. List in such format → [{column_name1: value11, column_name2: value12, …}, {column_name1: value21, column_name2: value22, …} …], where the dictionaries inside are the data frame values for each row.
      1. index
        1. Dictionary in such format → {index1: {column_name1: value1, column_name2: value2, …}, …}.
  • Define key
    • An optional boolean parameter, that provides a possibility to control the structure of created JSON. If turned off default name for the column will be “args
      If Define key option is chosen, then there are two more additional parameters to structure your JSON:
    • Key type
      • Parameter to choose whether the user option is placed as a column or as a key.
      • column
        • Modifies output column name.
      • key
        • Modifies output dictionary in a form {key: value} with key as the Key name and the value as a JSON, built in a specified way.
    • Key name
      • Parameter for setting key value that will be used as column or key in converted JSON.

Brick Inputs/Outputs

  • Inputs
    • Brick takes the dataset.
  • Outputs
    • Brick returns constructed JSON with a specified column name or default name of a column - “args”.

Example of usage

Let’s look at some simple examples of Prepare JSON brick usage.
The input dataset to the brick has the following structure:
notion image
We can form a JSON by connecting the dataset directly to the brick.
First, we select the ‘dict’ option with no key defined and run the pipeline.
notion image
As a result, we get the following output:
notion image
 
In the same way, we can use the other options.
list:
notion image
notion image

split:
notion image
notion image

records:
notion image
notion image

index:
notion image
notion image
If we check the ‘Define key’ flag, we choose column/key in the ‘Key type’.
Here, for instance, the column in the ‘Key type’ changes the output dataset’s column name into ‘abc’.
notion image
notion image
 
However, if we select key, we will get a different structure of the JSON.
notion image
notion image
 
 
As a more advanced example, we can take a part of a blockchain project that is running on Datrics.
notion image
Here we are using Prepare JSON brick twice because we need to structure our data on different levels. As an input, we have only multiplier and decimals columns.
notion image
Then we specify arguments in Prepare JSON brick in the following way:
notion image
As a result, we have:
notion image
Then we perform some basic data manipulations and move to another Prepare JSON brick, where we configure it in the following way:
notion image
In the output of this brick we get: