Skip to main content

Hi,

 

I am trying to pass data from another step into a code step. I’ve looked at the example online but can’t workout out to pass the data into the JSON body e.g.

const body = { "name": inputData.RateName}

 

Do I need to declare a variable for each input field?

 

Thanks

Hi @markoneill,

To pass JSON into a code step is fairly easy, while I’m sure there are other ways, I often use this technique.

First you need to parse the JSON string into a Javascript object then you can use it like any other object.

let jsonData = inputData.info.replace(/\r\n|\n/g, "\\r\\n");
let data = JSON.parse(jsonData);

// Now you can use the object in your new object

const body = { "name": data.RateName };

// if you want to output the object:
// output = newObject;

// If you want to return JSON instead of the
// let jsonStr = JSON.stringify(newObject);
// output = {json:jsonStr};

Note: I found new lines in the inputData string cause some errors so I always replace them before parsing the input.

 


Thanks @squibler - I figured it out shortly after posting.