Best answer

How to get json data in Code step?

  • 2 February 2022
  • 13 replies
  • 4411 views

I have some data coming in in json format with Zap.(Attached "Long Answer")
I want to use that json data in Code Step (JavaScript), but when I do JSON.parse, the test succeeds and the Zap execution fails.(Is the input format different between test and runtime?)
For example, the following code will fail.
// *input data
// {"object":"AAA", "id":"BBB"}

const fixed_json = JSON.parse(inputData.text);

output = {"result" : fixed_json.object};
How can I get the contents of the incoming json data with inputData?
icon

Best answer by mattc526 1 April 2022, 20:47

View original

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

13 replies

Userlevel 7
Badge +14

Hi @kt1111 

This worked when tested.

 

INPUT

 

OUTPUT

 

Hi @Troy Tessalone ,

Thank you for verifying.Is your result the result of actually launching the Zap scenario, not the test run?
Also, you input the value directly in the inputData, but what if you are referencing the value from another node as in my first figure?
Userlevel 7
Badge +14

@kt1111 

When you see {{placeholder}} it means the mapped variable was blank/null/empty/missing.

Check your Zap Runs for the DATA IN/OUT for each Zap step in a live triggered Zap Run: https://zapier.com/app/history/

 

@Troy Tessalone 

The data sent from the previous step is:


Is the input empty because the format that can be received by Code Step is different?

Userlevel 7
Badge +12

Hi @kt1111 - You'll need to explicitly map the different values from the Long Answer object in the code step because it can't accept objects.

Hi @ikbelkirasan ,

Does the inability to accept an object mean that I can't refer to the value from another step as shown in the image?
Or does it mean that the value entered in the Long Answer field is unacceptable due to the json format?

Userlevel 7
Badge +12

Hi @kt1111 

in your screenshot, it looks like Zapier has already parsed the object Long Answer and in the next zapier steps you will have data fields like Long Answer object, Long Answer id, Long Answer lead_price, etc… 

You will need to map the individual fields unless you rebuild or have access to the raw json from the previous step

Userlevel 1

Question - how do we include error checking in that code above? My zap keeps stopping because there is no data in the object. Can you add some code that would just skip it if the data is empty?

Userlevel 7
Badge +14

Hi @mattc526 

Good question.

A Filter step can be used to check if a variable exists: https://zapier.com/apps/filter/help

Userlevel 1

I appreciate the speedy reply Troy! Only issue is that I want the zap to run no matter what. Filters are good for stopping the zap if a condition isn’t met.

Basically, sometimes there is data in the object, and sometimes there isn’t. Even if there isn’t data there, I need the zap to continue firing. 

Userlevel 7
Badge +14

@mattc526 

Perhaps Paths: https://zapier.com/paths

Otherwise, you’ll have to add additional logic to the Code to handle if an input data point is null or not.

Check out this help topic:

 

Userlevel 1

Hi Troy (and anyone else that might need this) I got it!

let myInputData = inputData.text;
let finalData = "";

if (myInputData != null) {
finalData = {"result" : JSON.parse(inputData.text).name};
} else {
finalData = {"result" : ""};
}

output = finalData;

This code is when metadata comes over from stripe and needs to be parsed ONLY when the metadata is present. If the metadata isn’t there, then the output is empty. If it is there, then we output only the “name” that comes from the json array from stripe. Hope that helps!

Userlevel 7
Badge +9

Thanks so much for sharing your solution with the community, @mattc526! I know this is definitely going to help others. 🤗