Splitting Multiple Values in a Key into Separate Events

Last updated on Jan 05, 2024
On This Page

In this example, we will split multiple values received in a key and store them as Events in the same Event Type.

Consider the following sample Event, where a list of comma-separated values is received in the photo_ids and the photo_files keys:

{
    "event_name": "Photo Details",
    "properties":
    {
        "photo_ids": "008807, 0088072, 0088073",
        "photo_files": "008807.jpg, 008807b.jpg, 008807c.jpg"
    }
}

The following Transformation script:

  • Splits the multiple values in each key into Events, where each Event contains a value from photo_ids and its corresponding value from photo_files.

  • Creates the Event Type, photos containing these Events.

from io.hevo.api import Event

events = []

def transform(event):

    eventName = event.getEventName()
    properties = event.getProperties()
 
    if 'photo_files' in properties.keys() and len(properties['photo_files']) > 0:
        photo_files = properties['photo_files'].split(',')
        photo_ids = properties['photo_ids'].split(',')
 
 
        for ele in range(len(photo_ids)):
            new_properties = {}
 
            new_properties['id'] = photo_ids[ele].strip()
            new_properties['file_name'] = photo_files[ele].strip()
 
            events.append(Event('photos', new_properties))

    return events

The output from the above snippet is:

[
	{
		"event_name": "photos",
		"properties": {
			"file_name": "008807.jpg",
			"id": "008807"
		}
	},
	{
		"event_name": "photos",
		"properties": {
			"file_name": "008807b.jpg",
			"id": "0088072"
		}
	},
	{
		"event_name": "photos",
		"properties": {
			"file_name": "008807c.jpg",
			"id": "0088073"
		}
	}
]

As you can see above, the Event Type, photos contains three Events.



Revision History

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

Date Release Description of Change
Oct-25-2022 NA New document.

Tell us what went wrong