Flow designer action, parse JSON rest response

Chris5
Kilo Expert

I have created two custom actions, one that gets a Token from our AD and one that adds users to a specific group using that token. The call returns a status of 0 if it fails or 1 if success. I want an if condition so if it fails I can be alerted.

 

find_real_file.png

The above flow does not work. I added the pills with the values I want to the log and it looks like my script to parse the JSON is not working because no values are being returned. I know I am parsing the JSON correct when I get the token because I am able to pass it to the next action.

 

Here you can see no values returned.

find_real_file.png

 

 

Here you can see the call was successful.

find_real_file.png

 

This is my action with the script I am using to parse the JSON.

find_real_file.png

 

(function execute(inputs, outputs) {
		// Only parse the response body if the status code is 200	
    	if (inputs.status_code == '200'){
			
          	// Parse the response_body input variable and save parsed object as responseBody
          	var responseBody = JSON.parse(inputs.response_body);
			
          	// Map the parsed responseBody values to output variables
			outputs.samaccountname = responseBody.sAMAccountName;
			//outputs.objectsid = responseBody.objectSID;
          	outputs.userprincipalname = responseBody.userPrincipalName;
          	outputs.statusmessage = responseBody.statusMessage;
          	outputs.status = responseBody.status;
      }
})(inputs, outputs);
1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

Hi Chris,

 

From the attached screenshots it looks like your response body is an stringified array with a single object. If so can you try the following to parse the response:

(function execute(inputs, outputs) {
		// Only parse the response body if the status code is 200	
    	if (inputs.status_code == '200'){
			
          	// Parse the response_body input variable and save parsed object as responseBody
          	var responseBody = JSON.parse(inputs.response_body);
			
          	// Map the parsed responseBody values to output variables
			outputs.samaccountname = responseBody[0].sAMAccountName;
			//outputs.objectsid = responseBody[0].objectSID;
          	outputs.userprincipalname = responseBody[0].userPrincipalName;
          	outputs.statusmessage = responseBody[0].statusMessage;
          	outputs.status = responseBody[0].status;
      }
})(inputs, outputs);

 

--David

View solution in original post

3 REPLIES 3

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Are you sure you get responseBody.status as 0 or 1?

Can you check the real time log? What is the value associated with Status?

 

Thanks,
Ashutosh

In the third photo you can see the status response is 1, if I pass a bad username or bad group I get a 0 back for status. 

 

The problem is definitely parsing the JSON response and assigning the values to my outputs.

 

Here is the output data from a working call and parse.

find_real_file.png

Here is the one not working

find_real_file.png

DScroggins
Kilo Sage

Hi Chris,

 

From the attached screenshots it looks like your response body is an stringified array with a single object. If so can you try the following to parse the response:

(function execute(inputs, outputs) {
		// Only parse the response body if the status code is 200	
    	if (inputs.status_code == '200'){
			
          	// Parse the response_body input variable and save parsed object as responseBody
          	var responseBody = JSON.parse(inputs.response_body);
			
          	// Map the parsed responseBody values to output variables
			outputs.samaccountname = responseBody[0].sAMAccountName;
			//outputs.objectsid = responseBody[0].objectSID;
          	outputs.userprincipalname = responseBody[0].userPrincipalName;
          	outputs.statusmessage = responseBody[0].statusMessage;
          	outputs.status = responseBody[0].status;
      }
})(inputs, outputs);

 

--David