unpackdict
Separate values contained in dictionaries into columns in a data extract.
This guide explains how to configure the unpackdict instruction. To learn about another instruction, go back to the Available custom script instructions overview.
Introduction
Use the unpackdict instruction to separate values contained in dictionaries into columns in a data extract. The dictionary values are placed into new columns named after the dictionary keys.
Before you unpack the dictionaries, run the convertx transformation with the following Python expression: __import__('ast').literal_eval({FIELD})
Replace FIELD
with the name of the column that contains the dictionaries. Running this transformation enables Adverity to read the dictionaries. For more information, see the Python documentation.
Creating a custom script transformation using the unpackdict instruction
To create and configure a custom script using the unpackdict instruction, follow these steps:
-
In the Instructions step, select the unpackdict instruction.
-
To configure the custom script instruction, fill in the following fields. Required fields are marked with an asterisk (*).
-
Field*
-
Enter the name of the column that contains the dictionaries.
-
Keys
-
Enter the names of the dictionary keys to unpack. Adverity adds new columns to the data extract named after the dictionary keys entered into the Keys field. Adverity populates the separated dictionary values into these new columns. Specify as many keys as the number of keys in the longest dictionary.
Leave the Keys field empty to let Adverity automatically separate the dictionary values from the keys.
-
Includeoriginal
-
Select this checkbox to keep the column that contains the dictionaries in the data extract.
-
Samplesize
-
If you have not defined any keys in the Keys field, enter the number of rows where Adverity searches for keys and values to separate.
-
Missing
-
Enter a value with which Adverity populates empty columns. Use this option if the dictionaries have different lengths.
-
Subtable
-
Enter the name for a subtable that you want to use within this custom script.
A subtable is a temporary table that only exists for this custom script. You can apply additional instructions within the same custom script to the subtable. However, the subtable cannot be used in any other custom scripts.
If a subtable does not exist for the current custom script, the transformation is applied to the data extract, and the enriched data is output into the subtable. If the subtable already exists for the custom script, the subtable is used as the input for the transformation and optionally as the output.
Example 1 - Unpacking a dictionary with defined keys
Transformation configuration
-
Field*
-
Users
-
Keys
-
Name
Email
ID
-
Includeoriginal
-
Select this checkbox.
-
Missing
-
null
Data table before transformation
Clicks |
Users |
---|---|
1946 |
{'Name':'Jane Doe', 'Email':'j.doe@acme.com', 'ID':'AD-17'} |
3901 |
{'Name':'Joanne Bloggs', 'Email':'j.bloggs@acme.com', 'ID':'AE-23'} |
4958 |
([A, B, C], [D, E, F]) |
Data table after transformation
Clicks |
Users |
Name |
|
ID |
---|---|---|---|---|
1946 |
{'Name':'Jane Doe' |
Jane Doe |
j.doe@acme.com |
AD-17 |
3901 |
{'Name':'Joanne Bloggs', |
Joanne Bloggs |
j.bloggs@acme.com |
AE-23 |
4958 |
([A, B, C], [D, E, F]) |
null |
null |
null |
Example 2 - Unpacking a dictionary with no defined keys
Transformation configuration
-
Field*
-
Users
-
Includeoriginal
-
Select this checkbox.
-
Samplesize
-
100
-
Missing
-
null
Data table before transformation
Clicks |
Users |
---|---|
1946 |
{'key_1':'Jane Doe', 'key_2':'j.doe@acme.com', 'key_3':'AD-17'} |
3901 |
{'key_1':'Joanne Bloggs', 'key_2':'j.bloggs@acme.com', 'key_3':'AE-23'} |
4958 |
([A, B, C], [D, E, F]) |
Data table after transformation
Clicks |
Users |
key_1 |
key_2 |
key_3 |
---|---|---|---|---|
1946 |
{'key_1':'Jane Doe', |
Jane Doe |
j.doe@acme.com |
AD-17 |
3901 |
{'key_1':'Joanne Bloggs', |
Joanne Bloggs |
j.bloggs@acme.com |
AE-23 |
4958 |
([A, B, C], [D, E, F]) |
null |
null |
null |