Question

How do I pass a whole object to another step?

  • 29 November 2022
  • 7 replies
  • 1047 views

Userlevel 1

I’m working with the Asana updated trigger, but this question is application to any step returning json data. 

Their API returns an object like:
 

{
"data": {
"foo": "hello",
"bar": "apple",
"custom_fields": [
{
"biz": "pie",
"buz": "fruit"
},
{
"biz": "cake",
"buz": "chocolate"
}
]
}
}

I want to use a code step to extract the value of buz for the object that has a biz property of “cake”. When I click in to a following code step and attempt to insert data, I want to pass in the entire custom_fields object. However, the only results I see are fields that are full terminal paths e.x. data.custom_fields.buzdata.custom_fields.biz, data.bar, etc.

How can I pass in the entire custom_fields object to a following action?

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

7 replies

Userlevel 6
Badge +8

Hi @mbowser!

I was able to successfully pass the entire object without using a terminal path. Not sure why you’re not having the same success. Can you perhaps share more screenshots of your code so see what might be going on?

var customFields = nameOfJsonVariable.data.custom_fields;

output = [{customFields}];

 

Userlevel 1

Here is an example:
1. Create a code action with the following code like so:
 

output = {
data: {
foo: "hello",
bar: "apple",
custom_fields: [
{
biz: "pie",
buz: "fruit"
},
{
biz: "cake",
buz: "chocolate"
}
]
}
};

 

  1. Create an additional code action after the one you created in step 1. In the input data search, you should see the following:


    I do not see in the available list the custom_fields array, only child properties.
Userlevel 6
Badge +8

Ah, I see.

So I have a solution. It perhaps isn’t the prettiest, but it works.

You can use Javscript stringify() and parse() functions (one in each of the two code steps) to convert the JSON object to a string and then convert it back to an object in the next code step.

See screenshots below.

Code Step #1

Code Step #2

In code step #2, var data now holds the entire custom_fields object, which you can continue to manipulate however you’d like.

Userlevel 1

The step 1 I provided is a placeholder for an Asana integration action that does a GET. For the purposes of this example, step 1 should be considered immutable. Generically speaking, what I’m looking for is a way to pass a property that has a value that is a complex object (vs. a primitive) from one action to another. Something like 

 

Userlevel 6
Badge +8

Is the Asana step a Code by Zapier, Webhooks by Zapier, or native Asana action?

If it’s a Code by Zapier action, there’s no reason the object from your GET request must be considered immutable...even if you don’t want to make changes to the original complex object, you can save just the custom_fields object to a new variable using stringify, as mentioned above.

If it’s a webhooks or Asana action, then the data truly is immutable and I can completely understand the source of your frustration.

Will the same custom field names (Biz and Buz in this example) always exist in each record, or does it vary for each call?

Userlevel 6
Badge +8

If you know the custom field names will be the same for every call, you can rebuild the object in a subsequent code step like this:

 

Userlevel 1

It is a native Asana action. Those fields will always exist for each entry, so I think your recent solution will work! While I do wish Zapier had the functionality to pass around unflattened objects, I’m happy to find something that I can continue with. Thank you for your help!