Skip to content

array_to_dict_reducer

Hassan Syyid edited this page Feb 11, 2021 · 2 revisions

array_to_dict_reducer

Combines array of JSON dictionaries to a single dictionary with an aggregated value.

Definition

array_to_dict_reducer(key_prop, value_prop):
"""
    :param key_prop: property in dictionary for key
    :param value_prop: property in dictionary for value
    :return: a dictionary that has all the accumulated values
"""

Example

Input

Suppose we had the following dataset.

array_to_dict_reducer example input

Observe column CustomField with serialized JSON data in the following format:

[{"DefinitionId": "1", "Name": "Crew #", "Type": "StringType", "StringValue": "102"}]

Objective

To explode this, we'll need to reduce this as we only care about the Name and StringValue properties. We can use the explode_json_to_cols function with an array_to_dict_reducer to accomplish this.

# Grab the string value of entries
invoices = invoices.pipe(gs.explode_json_to_cols, 'CustomField', reducer=gs.array_to_dict_reducer('Name', 'StringValue'))
invoices[['Id', 'CustomField.Crew #']].head()

Output

final result

Clone this wiki locally