Cast CSV String as Dictionary

Hi,

i have the following Issue:
I get an CSV String as Response of an HTTP Request.
I am not sure how to cast it properly to a Dictionary in UiPath.
As a solution i am relying at the moment on a small python script. But this is not a good solution since i would always need to have python installed somewhere.

Does anyone knows how to do this in UiPath:

import json
import csv
from io import StringIO

def csv_to_dict(response):
    # Decode the response to UTF-8
    # respContent = response.decode('utf-8')
    # Cast the Decoded Response to String
    csvText = StringIO(response)
    # Read out the CSV
    reader = csv.reader(csvText, delimiter=',')

    # Create a Helper Object
    listObj = {}
	
    #Cycle through all result entries and put them into the list
    for num, row in enumerate(reader):
        listObj[num] = row
        # create the result dict
    result = {}    
	
    # fill the result Dict
    for i in range(len(listObj[0])):
        result[listObj[0][i]] = listObj[1][i]

    # Return the Result Dict
    return str(result)
1 Like

Using the uipath activities, you’d likely want to cast it to a datatable.
You may want to check out the Generate Data Table activity. It takes a string in and you can specify the separators to get a datatable representation of the csv data.
If you make your unique key the primary column of the data table you can also use it like you would a dictionary with the Find method. See here for a primary key example.

Hi,

Hope the following helps you.

Main.xaml (9.4 KB)

Regards,

Hi,

thanks for the quick solution.
It is quite helpful to see how it is done the UiPath way.

Best Regards,
Benjamin

1 Like