Oracle Service Bus 12.2.1 provides a JavaScript action, which allows us to include snippets of JavaScript code to be executed during proxy pipeline processing.
The most common case for using JavaScript is when dealing with JSON objects in REST services. Rather than converting the payload to XML and using XQuery or XSLT for manipulation, using JavaScript allows us to manipulate the JSON object directly. The JavaScript engine used in Service Bus also allows us to easily reference XML elements, making it easier to handle both JSON and XML-style payloads in JavaScript. JavaScript is not limited to REST services. We can use JavaScript in any service.
Here, we will see an example to read JSON payload and respond with JSON payload in OSB proxy pipeline using JavaScript action. The requirement is to get firstName and lastName from the Request JSON payload and return fullName by concatenating the firstName and lastName in the response using JavaScript expression.
JSON Request:
{
“customer”:
{
“firstName” : “Kasim”,
“lastName”: “V”
}
}
For retrieving firstName, lastName from JSON request and concatenate to fullName, JavaScript Expression in Request stage as below
.
To read and update variables, Service Bus binds a globally-scoped object called process. We invoke variables in expressions using dot (.) notation, like process.body or process.fullName.
In the Response stage to form JSON Response used below JavaScript expression.
Here we are assigning JSON response to $body using JavaScript expression.
Testing:
Request:
Response:
Pipeline Flow:
Hi ,
I’m getting Java callout output as Json string which is stored in $result variable in the pipeline ..
I want this to be shown as response body . So i followed your post , did the below in javascript .
process.body=process.result but this is not working ..
$result is having Json values. Can you help me