Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to separate json response

AnilM99
Tera Expert

Hi Team,

 

I got the result like below:

[{"title":"Students","link":"https:\/\/google.com\/wiki\/students\/"},{"title":"Control","link":"https:\/\/google.com\/wiki\/control\/"},{"title":"IQ- Engine","link":"https:\/\/google.com\/wiki\/iq-engine\/"}]

 

My requirement is how to load the flow designer outputs variables individually 

Like.,

title : Students Link : https:\/\/google.com\/wiki\/students\/

title : Control, Link : https:\/\/google.com\/wiki\/control\/

title : IQ- Engine, Link : https:\/\/google.com\/wiki\/iq-engine\/

 

Help me on the same

Thanks!

1 ACCEPTED SOLUTION

SunilKumar_P
Giga Sage

Hi @AnilM99, can you try below?

 

var response = '[{"title":"Students","link":"https://google.com/wiki/students/"},{"title":"Control","link":"https://google.com/wiki/control/"},{"title":"IQ-Engine","link":"https://google.com/wiki/iq-engine/"}]';

var parsedResponse = JSON.parse(response);

var firstObj = parsedResponse[0];
var secondObj = parsedResponse[1];
var thirdObj = parsedResponse[2];



Regards,
Sunil

View solution in original post

5 REPLIES 5

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

Strings can be transformed into a JSON object. This requires the use of the JSON.parse() function. The resulting object in your case will be an array of objects which you can iterate through:

var result = '[{"title":"Students","link":"https:\/\/google.com\/wiki\/students\/"},{"title":"Control","link":"https:\/\/google.com\/wiki\/control\/"},{"title":"IQ- Engine","link":"https:\/\/google.com\/wiki\/iq-engine\/"}]';
var resultObject = JSON.parse(result); // -> this transforms it into an object)
for(var i = 0; i<resultObject.length; i++){
    gs.log(resultObject[i]); //-> this will show each of the objects in your array which will have the keys "title" and "link" with their respective values
}

 

Hope this helps

Regards

Fabian

Hi @Fabian Thanks for the reply,

I tried the same script but i can't load 3 values in flow designer outputs variables, loaded only one value

 

Title : Students, Link : https:\/\/google.com\/wiki\/students\/

 

Thanks

Akshay Gupta2
Kilo Sage

Hi @Anil

 

You can have a custom action where you can have three output variables. Also with JSON.parse(), You can parse the result and assign to these output variables individually.

 

// Assuming 'result' contains the JSON result you provided

// Parse the JSON result
var data = JSON.parse(result);

// Loop through each item in the data array
data.forEach(function(item) {
    // Extract title and link from each item
    var title = item.title;
    var link = item.link;
    
    // Display the extracted data
    console.log("Title: " + title + ", Link: " + link);
    // You can also display this data in HTML or any other format as needed
});

Regards,

Alkshay

SunilKumar_P
Giga Sage

Hi @AnilM99, can you try below?

 

var response = '[{"title":"Students","link":"https://google.com/wiki/students/"},{"title":"Control","link":"https://google.com/wiki/control/"},{"title":"IQ-Engine","link":"https://google.com/wiki/iq-engine/"}]';

var parsedResponse = JSON.parse(response);

var firstObj = parsedResponse[0];
var secondObj = parsedResponse[1];
var thirdObj = parsedResponse[2];



Regards,
Sunil