JSON & Object Handling
Functions for processing and transforming JSON and object data in pandas DataFrames
explode_json_to_cols
Converts JSON array columns into separate columns, with one column per array value.
Usage
Parameters
df
(pd.DataFrame): Input DataFramecolumn_name
(str): Column containing JSON arraysdrop
(bool): Whether to drop original columninplace
(bool): Modify DataFrame in placeparser
(function): Custom parsing function (optional)
Returns
DataFrame with array values expanded into separate columns
explode_json_to_rows
Takes a column containing an array of objects and expands it into multiple rows, creating columns for each object property.
Usage
Parameters
df
(pd.DataFrame): Input DataFramecolumn_name
(str): Column containing JSON arraysdrop
(bool): Whether to drop original columnmax_level
(int): Maximum nesting level for flatteningparser
(function): Custom parsing function (optional)
Returns
DataFrame with array objects expanded into separate rows
json_tuple_to_cols
Convert JSON tuple columns into separate columns based on key-value pairs. Useful for transforming nested JSON data into a flattened DataFrame structure.
Installation
Basic Usage
Parameters
df
(pd.DataFrame): Input DataFramecolumn_name
(str): Column containing JSON tuplescol_config
(dict): Configuration for key-value mappingcols
: Output column nameslook_up
: Input property names
Returns
DataFrame with JSON tuple column split into separate columns
compress_rows_to_col
Compresses previously exploded rows back into a single column containing array data.
Usage
Parameters
df
(pd.DataFrame): Input DataFramecolumn_prefix
(str): Prefix of columns to compresspk
(str): Primary key for grouping rowsuse_csv
(bool): Use CSV format for storage
Returns
DataFrame with specified columns compressed into array format
array_to_dict_reducer
Creates a reducer function that converts arrays into dictionaries using specified key-value properties.
Usage
Parameters
key_prop
(str): Property to use as dictionary keyvalue_prop
(str): Property to use as dictionary value
Returns
Function that reduces arrays to dictionaries
Notes
- Returns a reducer function to be used with functools.reduce
- Raises AttributeError if values aren’t dictionaries
- Handles both specified key-value pairs and full dictionary merging
clean_obj_null_values
Replaces null values with None in stringified objects for further processing.
Usage
Parameters
obj
(str): Stringified dictionary/list with null values
Returns
String with ‘null’ values replaced with ‘None’
Notes
- Returns empty dict () for pandas NA values
- Particularly useful before using explode functions
- Preserves original object structure for non-null values