Skip to main content

Hi, i am trying to use code app by zapier, specifically javascript. i have a form that will send me an input of list disabilities. depending on the choices, the form send me data seperated by a comma (E.g. mental health, physical, emotional) 

the intent is to then turn this into a JSON format so i can send it somewhere else.

The expected output i need is :

!--scriptorstartfragment-->

{

  "disability_type”: "mental illness",

  "disability_type": "physical",

  "disability_type": "emotional"

}

!--scriptorendfragment-->

 

no matter how i try, zapier return/output is not returning me JSON format. did i miss anything?

 

Hi ​@Avelyn 

TIP: Try asking ChatGPT for help with the JavaScript Code.


Hi Troy, did that before posting the question. the output from Zap is then either single output (Or if i add an index then assign value, it will give me the all 3 output), BUT it render out the output to show 1 field = 1 value, instead of the JSON format value that i wanted.

 

E.g. code:

 

var input = inputData.sDisabilities;

const values = input.split(',');

 

const result = values.reduce((acc, value, index) => {

  acc `disability_type_${index + 1}`] = value.trim();

  return acc;

}, {});

 

return result;

 

Zapier output:

 

Disability Type 1

mental illness

Disability Type 2

physical

Disability Type 3

emotional

 

It seems to be a zapier specific issue and how zapier is handling the code, hence i am asking if anyone have seen/how to mitigate this.


@Avelyn 

For us to have true context, post these screenshots:

  • how your Zap steps are outlined
  • how your Zap steps are configured in EDIT mode with the field mappings visible
  • how the DATA OUT is returned from the Zap step 1 that you are trying to use in the Code step

Hi ​@Avelyn,

Below is the code you can use to get the expected results:

//Get the comma-separated disability list from the previous Zapier step
let disable_list = inputData.disable_list || '';

let items = disable_list
.split(',')
.map(item => item.trim())
.filter(item => item);
let keyValuePairs = =];


items.forEach(value => {
let pair = `"disability_type":"${value}"`;
console.log(pair);
keyValuePairs.push(pair);
});
let outputText = `{ ${keyValuePairs.join(', ')} }`;

return {output: outputText, input: disable_list};

 

generated result

Let me know if you need any further help!
And if this helped, feel free to drop a quick reply so others know it worked for you. 👍


Hey ​@Avelyn 👋

Did the Javascript code Probotic Solutions suggested do the trick here?

Want to make sure you’re all set, so please let us know how it went! 🙂