Need to Update Json body before aggregation in webservice connector

Hi All,
Please help me with your inputs on below requirement
We have an aggregation end point which gives response as below, where entitlements are scattered as individual key and value. I need to modify it to the expected json format.

Can i use beforeoperation rule for this?

Json response body from API

 {
        "name": "[email protected]",
        "admin": true,
        "api": true,
        "projectAdmin": true,
        "readOnly": false,
        "currentLoggedInUser": false
    }

Where admin,api,projectadmin,readonly are the access of the user(true/false say whether user have those access)
i need to modify it to below(roles hold only the access marked as true) in order to mark entitlements as multivalued.
Expected Json:-

 {
        "name": "[email protected]",
        "roles": [admin,api,projectAdmin],
        "currentLoggedInUser": false
    }

You would want to use an After Operation Rule.

The Account Aggregation would run, and then with the After Operation Rule you would take the rawResponseObject modify it to the JSON you want, and then return the modified JSON.

You will probably need to write other rules for other HTTP Operations like Get Object, Add Entitlement, and Remove Entitlement.

Hi @Carlatto ,
Thank you for prompt reply!
We are performing only account aggregation and we don’t have Get Object , Add Entitlement , and Remove Entitlement Http Operations.
Do we need to create Get Object , Add Entitlement , and Remove Entitlement Http operations?

Can Aggregation Http operation Response mapping have modified json Key value pair?

Hi @Divya_Kusumula03 ,

Welcome to the Developer Community :wave:

While you cannot modify the response object , but you still be able to achieve this use case .
Write an after operation for aggregation operation. You need to have an understanding on Bean Shell script for this.

Refer to Web Service After Operation Rule
The following is a piece of code which gives you an idea on how to implement the logic:

UserIDs in the below ode is a list of all the user id’s . The loop iterates over all the accounts existing in the system .

List user_map_data =  new ArrayList();
	log.info("Collected All Account IDs : List of IDS" + UserIDs);
	for(int i=0;i<UserIDs.size();i++)
	{	
		
			Map user = new HashMap();
			String userid =UserIDs.get(i);
			String getUser = "your api endpoint to  get single user response ";
			log.info("executing end point " +getUser);
			String response_in_String = restClient.executeGet(getUser,headers,allowedStatuses);
			Object response = JsonUtil.parseJson(response_in_String);
			log.info("response of one user :"+response);
			String name = response.get("username");
			String admin = response.get("admin")+"";
			String api = response.get("api")+"";
			List allRoles = new ArrayList();
			if(admin=="true"){
			allRoles.add("admin");
			}
			if(api=="true"){
			allRoles.add("api");
			}
			
			Map user_map = new HashMap();
			user_map.put("name",name);
			user_map.put("roles",allRoles);
			user_map_data.add(user_map);
			
	}
processedResponseObject.addAll(user_map_data);

This should help in resolving your query !

Thanks!

1 Like

Hi @sidharth_tarlapally

This is regarding the AfterOperation Webservice rule

i wrote below rule , but the issue is it is aggregating 1 user instead of 96 users and that one user has multiple roles instead of 4

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
Map updatedMapInfo = new HashMap();
Map newProcessedResponse = new HashMap();
Map userMap = new HashMap();
ArrayList userRoleArray = new ArrayList();
List Finallist = new ArrayList();

if (rawResponseObject != null) {
JSONArray results = new JSONArray(rawResponseObject);
log.error(“RULEWS results at start” + results);
for (int i = 0; i < results.length(); i++) {
JSONObject user = results.getJSONObject(i);
if(user.getString(“name”) != null){
Boolean admin = user.getBoolean(“admin”);
Boolean api = user.getBoolean(“api”);
Boolean projectAdmin = user.getBoolean(“projectAdmin”);
Boolean readOnly = user.getBoolean(“readOnly”);
Boolean currentLoggedInUser = user.getBoolean(“currentLoggedInUser”);
if(admin)
userRoleArray.add(“admin”);
if(api)
userRoleArray.add(“api”);
if(projectAdmin)
userRoleArray.add(“projectAdmin”);
if(readOnly)
userRoleArray.add(“readOnly”);
if(userRoleArray.size()>0){
userMap.put(“Email”, user.getString(“name”));
userMap.put(“Roles”, userRoleArray);
Finallist.add(userMap);

}
}
else
log.error(“RULEWS no email found for user”);
}
}
log.error(“RULEWS userMap at end” + userMap);
log.error(“RULEWS Finallist at end” + Finallist);
updatedMapInfo.put(“data”, Finallist);
log.error("RULEWS processedResponseObject Before is " + processedResponseObject);
return updatedMapInfo;

Hello @Divya_Kusumula03

If you don’t place braces around the code controlled by the if statement, only one statement will be controlled by the if statement, and subsequent statements will be executed unconditionally , no matter how you might indent the code.

Thus , you need to modify your code near if statements in the following way:

if(admin){
userRoleArray.add(“admin”);
}
if(api){
userRoleArray.add(“api”);
}
if(projectAdmin){
userRoleArray.add(“projectAdmin”);
}
if(readOnly){
userRoleArray.add(“readOnly”);
}

Please ensure that your code indentations and braces closings are aligned properly. Refer to this example rule on how to handle the rawResponseObject for iterating it over all the records.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.