Merging 2 JSON Array into One in OIC using XSLT
We encountered a requirement wherein a target system’s API would return a set of data, and we would have to call another API to obtain the remaining data. As a result, we are getting 2 response payloads from the target system, which we must combine into a single JSON structure in order to use them for additional processing.
There are several way to achieve this. The easiest option is to load those data into DB, then using a PLSQL procedure, we can fetch the data. But in this technical approach, we will try to avoid using database to achieve the required payload. In this blog, I will be using single payload with 2 JSON array structure. An example of JSON would be this:
{
"TEST" : "",
"nameAPI" : [ {
"empId" : "emp001",
"name" : "Vinoth KS"
}, {
"empId" : "emp002",
"name" : "Biman Dey Sarkar"
} ],
"roleAPI" : [ {
"empId" : "emp001",
"jobRole" : "Human Resource",
"location" : "Hyderabad"
}, {
"empId" : "emp002",
"jobRole" : "Integration Lead",
"location" : "Bengaluru"
} ]
}
As you can see, the first JSON array has empId and Name. The Second JSON array have empId, jobRole & location. And our target JSON will be as follows:
{
"rslt" : [ {
"name" : "Vinoth KS",
"role" : "Human Resource",
"location" : "Hyderabad"
}, {
"name" : "Biman Dey Sarkar",
"role" : "Integration Lead",
"location" : "Bengaluru"
} ]
}
Now the problem starts when you try to map 2 repetitive element into 1 repetitive element in OIC mapper. In below screenshot, I already mapped Name API repetitive element with target payload, hence, once I try to map Role API it will not allow me to perform with a pop-up “Target node is already mapped.”
Hence, we need to achieve it using code mode. At first, I will drag the Role API which we are planning to use as nested loop. And we will take a backup of loop in code mode. We normally do this to avoid making any mistake in namespace of the source API. In below screenshot, we will take the back-up for the entire for loop and then again delete the mapping in design view.
Now again will go to design mode and complete the mapping for the outer loop.
Now open the mapper in code mode and paste the nested for loop inside the for loop generated for Name API.
Option 1: Now you have to create a relationship between the outer loop and inner loop. For that, we will create a variable currEmpId and store the empId of current iteration. And then inner loop will be executed only when empId of inner loop matches with the outer loop. But please make sure you complete all the mapping before declaring a variable. As once you declare the variable you can’t access in design mode.
Option 2: In the previous approach, we had a foreign key between two arrays. But we might encounter a scenario where we will not have any foreign key. In that case, if the target system confirms that they will send data in both APIs in the same sequence, then only we can take this approach. So what we have done here is, instead of taking empId of the current iteration, we are taking the count of the iteration.
I hope you will find this article useful if you ever encounter such a situation. As I attempted to run the integration with two distinct JSON arrays, the outcome is seen in the screenshot below. In a real-time scenario, OIC will receive such payload as a response from 2 different API, and using similar approach, you can combine them into one JSON array.
Thank you.