csakon
csakon

Reputation: 611

Return an unparsed list of dictionaries in Zapier Code

I have successfully created code that reads from my form trigger how many managers and members (arbitrary roles in a company) are in the form. Then it creates an array successfully that looks like this:

"managers": [
   {
      "full_name": “Manager One“,
      "email": “[email protected]”,
      "address": “111 Manager St“,
      "city_st_zip": “Manager, Texas 11111”
    }, 
   {
      "full_name": “Manager Two“,
      "email": “[email protected]”,
      "address": “222 Manager St“,
      "city_st_zip": “Manager, Texas 22222”
    }
]

The code step works just great and I've confirmed it returns what I want. The issue is that I need to implant it into some JSON I'm formatting to send elsewhere via webhook. I was expecting the Code Step to have the blob of code ready to go for me to just insert into the Custom Webhook Request like:

{
      "name": “ABC Company”,
      "managers": [
       {
          "full_name": “Manager One“,
          "email": “[email protected]”,
          "address": “111 Manager St“,
          "city_st_zip": “Manager, Texas 11111”
        }, 
       {
          "full_name": “Manager Two“,
          "email": “[email protected]”,
          "address": “222 Manager St“,
          "city_st_zip": “Manager, Texas 22222”
        }
      ],
      "members": [
        {
          "full_name": “Member One”,
          "email": “[email protected]”,
          "address": “111 Member St“,
          "city_st_zip": “Member, Texas 11111”
          "invested": 100,
          "stake": 50
        },
        {
          "full_name": “Member Two”,
          "email": “[email protected]”,
          "address": “222 Member St”,
          "city_st_zip": “Member Texas 22222”
          "invested": 100,
          "stake": 50
        }
      ],
      "taxes_manager": “Taxes Manager“,
      "business_purpose": “make money“,
      "principal_office_street": “123 Main St”,
      "principal_office_city_st_zip": “Principal, Texas 78701“,
      "state": "TX",
  }

The problem is that Zapier parses the list of dictionaries into individual variables instead of just giving me the whole block of code as shown here

enter image description here

How do I insert the list of dictionaries into the JSON as I need?

Upvotes: 1

Views: 223

Answers (1)

xavdid
xavdid

Reputation: 5262

David here, from the Zapier Platform team. Great question!

You've got two main options:

  1. Do the request in the code step itself. There are docs here. This is great because functionality stats together and it's easy to troubleshoot
  2. Instead of returning managers, return {"payload": json.dumps(managers)}, which will return a string instead of an object. We don't do any extra processing on strings, so you can use it in another step. Note that if you do this, the following step needs to be Custom Request, not POST (because the latter warns not to put raw JSON strings in there since we parse it again).

Both of these options have the same effect, though #2 will cost you an extra task on your usage. Depending how close to limits you are, this may or may not matter.

​Let me know if you've got any other questions!

Upvotes: 2

Related Questions