unpackdict
Separate values contained in dictionaries into columns in a data extract.
Introduction
Use the unpackdict enrichment 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 enrichment with the following Python expression: __import__('ast').literal_eval({FIELD})
Replace FIELD
with the name of the column that contains the dictionaries. Running this enrichment enables Adverity to read the dictionaries. For more information, see the Python documentation.
Creating the enrichment
For more information on creating an enrichment, see Using custom scripts.
Configuring the enrichment
To configure the enrichment, 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 contain the enriched data. The enrichment is applied to the whole data extract, then the enriched data is output into the subtable you have named here.
This subtable is a temporary table, which means it 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.
Example 1 - Unpacking a dictionary with defined keys
Enrichment configuration
-
Field*
-
Users
-
Keys
-
Name
Email
ID
-
Includeoriginal
-
Select this checkbox.
-
Missing
-
null
Data table before enrichment
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 enrichment
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
Enrichment configuration
-
Field*
-
Users
-
Includeoriginal
-
Select this checkbox.
-
Samplesize
-
100
-
Missing
-
null
Data table before enrichment
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 enrichment
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 |