Skip to main content
Best answer

How to get json data in Code step?


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?

Best answer by mattc526

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!

View original
Did this topic help you find an answer to your question?
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

Troy Tessalone
Forum|alt.badge.img+14

Hi @kt1111 

This worked when tested.

 

INPUT

 

OUTPUT

 


  • Author
  • Beginner
  • 3 replies
  • February 2, 2022

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?

Troy Tessalone
Forum|alt.badge.img+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/

 


  • Author
  • Beginner
  • 3 replies
  • February 3, 2022

@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?


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • February 3, 2022

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.


  • Author
  • Beginner
  • 3 replies
  • February 7, 2022

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?


GetUWired
Forum|alt.badge.img+12
  • Zapier Expert
  • 1030 replies
  • February 8, 2022

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


  • New
  • 4 replies
  • April 1, 2022

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?


Troy Tessalone
Forum|alt.badge.img+14

Hi @mattc526 

Good question.

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


  • New
  • 4 replies
  • April 1, 2022

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. 


Troy Tessalone
Forum|alt.badge.img+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:

 


  • New
  • 4 replies
  • Answer
  • April 1, 2022

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!


christina.d
Forum|alt.badge.img+9
  • Zapier Staff
  • 2653 replies
  • April 5, 2022

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