Best answer

Zapier "Run Python Code" Error - Issue parsing dictionary

  • 4 April 2024
  • 4 replies
  • 88 views

Userlevel 1
Badge

Hi everyone! I'm just learning how to write python, just started using Zapier and definitely see the opportunities for this being an amazing tool!

I am getting stuck on the following through.

I have the following data input:

countries = [
{"Country_Num": 1, "Country_Name": "United States"},
{"Country_Num": 2, "Country_Name": "United Kingdom"},
{"Country_Num": 3, "Country_Name": "Canada"},
{"Country_Num": 4, "Country_Name": "Germany"},
{"Country_Num": 5, "Country_Name": "France"},
{"Country_Num": 6, "Country_Name": "Australia"},
{"Country_Num": 7, "Country_Name": "Japan"},
{"Country_Num": 8, "Country_Name": "Brazil"},
{"Country_Num": 9, "Country_Name": "India"},
{"Country_Num": 10, "Country_Name": "South Africa"}
]

And I'm trying to turn this data into something useable in Zapier, so that I can add that data into a GoogleSheet.

Therefore, I'm using the Zapier step called "Run Python in Code by Zapier" and it allows me assign the input data above as "countries"

Now, I'm trying to use code to turn the data into two outputs that I can use using the following script:

# Accessing the input data which is provided as 'countries'
countries_data = input_data['countries']

# Initializing lists to hold country numbers and names
country_nums = []
country_names = []

# Iterating over each country in the input data
for country in countries_data:
# Appending the country number and name to their respective lists
country_nums.append(country["Country_Nums"])
country_names.append(country["Country_Names"])

# Preparing the output to include both lists
output = {'Country_Nums': country_nums, 'Country_Names': country_names}

return output

However, whenever I try to test the code, I get the following error:

Failed to create a run python in Code by Zapier
Your code had an error! Traceback (most recent call last): File "<string>", line 18, in the_function TypeError: string indices must be integers, not 'str'

Any help on this would be appreciated! Could this be because Zapier takes the data input as a string? 

icon

Best answer by luniko 14 April 2024, 19:38

View original

4 replies

Userlevel 1
Badge

I was able to figure it out.

The issue was the fact that regardless of what is put in as the input data, it comes in as its own value as a string in its own dictionary. 

So my original data goes from this:

countries = [
{"Country_Num": 1, "Country_Name": "United States"},
{"Country_Num": 2, "Country_Name": "United Kingdom"},
{"Country_Num": 3, "Country_Name": "Canada"},
{"Country_Num": 4, "Country_Name": "Germany"},
{"Country_Num": 5, "Country_Name": "France"}
]

To being interpreted as this:

{"countries": "countries = [
{"Country_Num": 1, "Country_Name": "United States"},
{"Country_Num": 2, "Country_Name": "United Kingdom"},
{"Country_Num": 3, "Country_Name": "Canada"},
{"Country_Num": 4, "Country_Name": "Germany"},
{"Country_Num": 5, "Country_Name": "France"}
]"}

Therefore, I had to get ChatGPT to change the code to the following:

import json


# Extract the JSON-like part from the string
countries_str = input_data['countries'].split("countries = ")[1].strip()

# Replace single quotes with double quotes to form valid JSON
countries_str = countries_str.replace("'", '"')

# Remove newline and extra spaces
countries_str = "".join(countries_str.split())

# Parse the JSON string into a Python list
countries_data = json.loads(countries_str)

# Initialize lists to hold country numbers and names
country_nums = []
country_names = []

# Iterate over each country in the input data
for country in countries_data:
# Append the country number and name to their respective lists
country_nums.append(country["Country_Num"])
country_names.append(country["Country_Name"])

# Prepare the output to include both lists
output = {'Country_Num': country_nums, 'Country_Name': country_names}

return output

This fixed it.

Userlevel 1
Badge

@Troy Tessalone

Thanks for the reply! I see what you’re saying with the keys. I’ve fixed them but I’m still getting the same issues.

Here are some screenshots of the Zap steps that are relevant:
 

  1. The Zap looks for the code from the Google sheet cell. Here’s what the test of the Zap returns.
  1. Next, I have the step “Run Python in Code by Zapier” running the following action:


    Here’s the code:

    # Accessing the input data which is provided as 'countries'
    countries_data = input_data['countries']

    # Initializing lists to hold country numbers and names
    country_nums = []
    country_names = []

    # Iterating over each country in the input data
    for country in countries_data:
    # Appending the country number and name to their respective lists
    country_nums.append(country["Country_Num"])
    country_names.append(country["Country_Name"])

    # Preparing the output to include both lists
    output = {'Country_Num': country_nums, 'Country_Name': country_names}

    return output
  2. When I test the code, it still yields this error:

 

I have tried ChatGPT, but I just end up going in circles and it can’t fix the issue. I’ve even posted on Stackoverflow and people are saying that they don’t see anything wrong with the Python code, but it might be something on Zapier’s end (Referring to a file "<string>")?

I’m truly stumped.

Userlevel 7
Badge +14

Hi @luniko 

For us to have full context, post screenshots with how your Zap Code step is configured.

One way to troubleshoot your code is to ask ChatGPT to check/fix based on the error.

 

The error you're encountering is because of a typo in accessing the keys of the country dictionary. You've used "Country_Nums" and "Country_Names", but the correct keys are "Country_Num" and "Country_Name". Here's the corrected version of your code:

Userlevel 7
Badge +14

@luniko 

If you need help with configuring the Code step, perhaps consider hiring a Certified Zapier Expert: https://zapier.com/experts

Reply