BookmarkSubscribeRSS Feed

How to access a DataGrid inside a Python code node in Intelligent Decisioning

Started ‎11-23-2021 by
Modified ‎12-07-2021 by
Views 5,280

In Intelligent Decisioning variables can have different data types. One data type is a DataGrid. A DataGrid is a table and allows us to build one to many relationships on decision records. For example, you could have a person with several addresses.

 

Decision Record:

Name Date of Birth Address    
Dave 1998-05-02 Street Town Postal Code
    10 High St Brighton BG1 3ER
    11 Downing Rd London  LO1 5YY

 

In the above example you would put the address information in a DataGrid and perform all necessary address operations on the DataGrid. For this Intelligent Decisioning supplies 70+ DS2 functions to work with DataGrids. There are functions for different categories like create, update, or delete, to get meta information, to retrieve values from a DataGrid, to perform statistical operations, and more.

 

However, sometimes we have decision flows where we must add some logic in a code node. If the code node is a Python code node and we want to work with a DataGrid in the Python code, we can’t use the DS2 DataGrid function, but we can still work with the DataGrid in Python.

 

Work with DataGrids in a Python code node

Let’s have look how to work with DataGrids in a Python code node in Intelligent Decisioning.

There are basically three steps involved:

  • Serialize the DataGrid to pass it into the Python code node.
  • In Python
    • Convert the serialized DataGrid into a Python list object
    • Work with the DataGrid in Python
    • Serialize the list object and return it back to the decision flow
  • Convert the serialized list object from Python into a DataGrid in the decision flow

Data Grid structure

Technically DataGrids are json objects and stored in a character variable on the decision record.

A DataGrid json string has the following basic format:

[{"metadata”: [column-definitions]},{"data":[column-data]}]

The column definitions are name-value pairs separated by commas:

{"column1-name":"data-type"},{"column2-name":"data-type"},...

The data for each row of the DataGrid is specified in square brackets with commas between each value:

[column1-data, column2-data...]

 

Pass DataGrid into Python code node

As we cannot pass a DataGrid object directly into a Python code node in Intelligent Decisioning, we need to serialize the DataGrid first and save its JSON structure in a character variable.

In this example I use a Rule Set. The Rule Set has one input parameter and one output parameter:

  • Input parameter:
    • The DataGrid to be serialized.
  • Output parameter:
    • The character variable to hold the DataGrid json structure.

ID_01.png

 

We add an assignment statement to the Rule Set to retrieve the json structure for the DataGrid by calling the DataGrid function dataGrid_toString() and pass the output of the function to the Rule Set output parameter.

dgJSON= dataGrid_toString(dg)

 

ID_02.png

 

Work with the DataGrid in Python

Next, we use the Rule Set in the decision flow before we call the Python node to prepare the DataGrid in order to input it into the Python code node.

 

ID_03.png

 

We pass the serialized DataGrid string (addressJSON) into the Pyhton code node as input parameter.

In the Python code we use json.loads() from the Pyhton json library to load the DataGrid into a Python list object.

Once we have loaded the DataGrid into a list object we have full access to the DataGrid and can perform all necessary operations.

 

In our case we validate each address row by calling an external address validation service. We add a new column to the DataGrid and store the validation result in the DataGrid.

 

ID_04.png

 

To send the changed DataGrid back to the decision flow we need to serialize its Python list object by calling json.dumps() and return the serialized object via Python return.

 

ID_05.png

 

 

 

 

 

Convert Python list object into DataGrid

In the decision flow we receive the updated DataGrid as serialized JSON string. We pass this JSON string into a Rule Set to convert it back into a DataGrid.

 

ID_06.png

 

The Rule Set has one input parameter and one output parameter:

  • Input parameter:
    • The character string holding the serialized Python list object.
  • Output parameter:
    • The DataGrid created from the serialized input string.

 ID_07.png

 

We add an assignment statement to the Rule Set to convert the json structure into a DataGrid by calling the DataGrid function dataGrid_create() and output the DataGrid from the Rule Set.

dataGrid_Create(dg, dgJSON)

 

ID_08.png

 

 

Now we can continue working with the updated DataGrid in the decision flow.

 

Summary

We discussed the steps involved working with DataGrids in a Python code node. This way we can conveniently use DataGrids in Python if needed which enhances the capabilities of Intelligent Decisioning.

 

Version history
Last update:
‎12-07-2021 05:46 AM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags