Python Code in Zapier: Trouble Parsing Dictionary Data

Hey everyone! I’m new to Python and Zapier. I’m trying to automate tasks but I’m hitting a snag.

I’ve got this list of countries:

nations = [
    {'Nation_ID': 'A', 'Nation_Title': 'Spain'},
    {'Nation_ID': 'B', 'Nation_Title': 'Italy'},
    {'Nation_ID': 'C', 'Nation_Title': 'China'},
    {'Nation_ID': 'D', 'Nation_Title': 'Mexico'},
    {'Nation_ID': 'E', 'Nation_Title': 'Russia'}
]

I want to use this in Zapier’s “Run Python Code” step to enter the data into a Google Sheet. My script is as follows:

nations_info = input_data['nations']

nation_ids = []
nation_titles = []

for nation in nations_info:
    nation_ids.append(nation['Nation_ID'])
    nation_titles.append(nation['Nation_Title'])

result = {'Nation_IDs': nation_ids, 'Nation_Titles': nation_titles}

return result

However, I keep receiving an error about string indices. I suspect Zapier might be interpreting my input as a string rather than a list of dictionaries. Does anyone know how I can convert the input from a string into an actual list of dictionaries? Thanks!

hey olivias, had same problem. try using ast.literal_eval() to convert the string to a list. import ast at the top, then do nations_info = ast.literal_eval(input_data[‘nations’]). should fix it. lmk if u need more help!

I’ve encountered a similar issue when working with Zapier’s Python code step. The problem likely stems from how Zapier handles input data. When you pass data into a Python step, it often comes in as a string representation of your data structure, not the actual structure itself.

To resolve this, you’ll need to parse the input string back into a Python object. Here’s a modified version of your script that should work:

import json

nations_info = json.loads(input_data['nations'])

nation_ids = []
nation_titles = []

for nation in nations_info:
    nation_ids.append(nation['Nation_ID'])
    nation_titles.append(nation['Nation_Title'])

result = {'Nation_IDs': nation_ids, 'Nation_Titles': nation_titles}

return result

The key change here is using json.loads() to parse the input string back into a Python object. This should resolve the string indices error you’re encountering.

Also, make sure that when you’re passing the data into the Python step, you’re formatting it as a valid JSON string. If you’re still having issues, double-check how you’re inputting the data in the previous step of your Zap.

I’ve faced this issue before in Zapier. The problem is likely that Zapier is indeed passing your data as a string. Here’s a solution that should work:

import ast

nations_info = ast.literal_eval(input_data['nations'])

nation_ids = [nation['Nation_ID'] for nation in nations_info]
nation_titles = [nation['Nation_Title'] for nation in nations_info]

result = {'Nation_IDs': nation_ids, 'Nation_Titles': nation_titles}

return result

This solution uses ast.literal_eval() to safely evaluate the string as a Python expression. It’s generally preferred over json.loads() for this type of data. I’ve also used list comprehensions to make the code more concise. Make sure you are passing the nations data correctly in the previous Zap step and verify that the input format matches what the script expects.