Best answer

inputData into JSON field

  • 26 September 2020
  • 2 replies
  • 1226 views

Userlevel 1

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

icon

Best answer by squibler 27 September 2020, 00:45

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.

2 replies

Userlevel 3
Badge +1

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.

 

Userlevel 1

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