Hey folks, I’m trying to figure out how to make a nested JSON in Zapier’s Code step. I want to use it with Formstack Documents.
Here’s what I’m dealing with:
- My trigger is when a Pipedrive deal moves to a new stage
- I need to output a merged Formstack Document with product details from the deal
Right now, Zapier splits up all the values by key name. But I want to group some of these (like name, code, unit, quantity, and category) so Formstack Documents can use its tablerow function to loop through the products.
I’m thinking of using a Python code step to combine these items. Then I’d pass this grouped data to Formstack Documents.
Has anyone done something like this before? Any tips on how to structure the data? I’m open to using Formstack’s REST API in the same code step if that’s easier.
Thanks for any help!
I’ve implemented something similar in my projects. One approach that worked well was using a list comprehension in Python to create the nested structure efficiently:
output = {
‘deal_info’: input_data[‘deal’],
‘products’: [{
‘name’: item[‘name’],
‘code’: item[‘code’],
‘unit’: item[‘unit’],
‘quantity’: item[‘quantity’],
‘category’: item[‘category’]
} for item in input_data[‘line_items’]]
}
return json.dumps(output)
This creates a JSON-compatible dictionary with deal info and a list of product dictionaries. It’s concise and should work seamlessly with Formstack Documents’ tablerow function.
Remember to import json at the top of your Code step. If you encounter any issues, double-check the input data structure and adjust the keys accordingly.
I’ve tackled a similar challenge before. Here’s what worked for me:
In the Code step, I used Python to create a list of dictionaries for the products. Something like this:
products =
for item in input_data[‘line_items’]:
product = {
‘name’: item[‘name’],
‘code’: item[‘product_code’],
‘unit’: item[‘unit’],
‘quantity’: item[‘quantity’],
‘category’: item[‘category’]
}
products.append(product)
final_output = {
‘deal_name’: input_data[‘deal_name’],
‘products’: products
}
return final_output
This structure should work well with Formstack Documents’ tablerow function. Just make sure to set the output of your Code step as the input for the Formstack Documents action.
If you’re still having trouble, you might want to check Formstack’s API documentation. Sometimes, working directly with their API can give you more control over the data structure.
i did smth similar. try grouping product data in a python dict then convert it to json; e.g.: products=[{‘name’: x, ‘code’: y, …}] then pass to formstack. best luck!