Convert JSON String to Dictionary Object

Last updated on Jun 30, 2023
Definition: jsonStringToDict(jsonString: str) → dict
Description: Converts a JSON string to a dictionary object
Parameters: jsonString: The JSON string which needs to be converted
Returns: A Python dict object created from the input JSON string

The following script converts a JSON string into a dictionary using the jsonStringToDict function. The resulting dictionary is then assigned to the <jsonDict> field.

Note: Replace the placeholder values in the script with your own values.

from io.hevo.api import Event
from io.hevo.api import Utils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()

    <jsonString> = "{<JSON string to be converted>"}"
    properties['jsonDict'] = Utils.jsonStringToDict(<jsonString>)
    ...

Sample Transformation

Consider a JSON string, customer with value \"name"\: \"Eric\" , \"exp\": \"15years\" , \"location\": \"USA\".

The jsonStringToDict function converts the customer JSON string into a dictionary, and assigns the resulting value to the withoutUnicode field. The resulting dictionary is then merged into the properties to form a single object containing the details.

from io.hevo.api import Event
from io.hevo.api import Utils

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()

    customer = Utils.jsonStringToDict(properties['customer'])

    for cust_key, value in customer.items():
        properties[cust_key] = value

    del properties['customer']

    return event

Sample Output

The JSON keys id, name, location, and exp are converted to individual fields and added to the properties.

Sample Output - Convert JSON String to Dictionary Object



Revision History

Refer to the following table for the list of key updates made to this page:

Date Release Description of Change
Jun-26-2023 NA New document.

Tell us what went wrong