Monday 9 May 2016

Oracle SOA Suite 12c: Retrieve data from JSON Using JavaScript Expressions in Service Bus 12C (OSB)

Service Bus provides a JavaScript action, which is used in pipelines The JavaScript action allows you to include snippets of JavaScript code to be executed during 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 you to manipulate the JSON object directly. The JavaScript engine used in Service Bus also allows you to easily reference XML elements, making it easier to handle both JSON and XML-style payloads in JavaScript.
Useful Expressions
Service Bus binds a globally-scoped object called process
To access a $body variable in the Service Bus message context, use an expression like the following:
process.body
To create a variable in the Service Bus message context, use an expression like the following:
process.newVar = …;
To delete a variable, use the JavaScript delete operator:
delete process.var;
To access a variable, use below JavaScript:
process.firstName = process.body.employees.firstName;
The following expression returns the value of the inbound HTTP Content-Type header:
process.inbound.ctx::transport.ctx::request.tp::headers.http::[“Content-Type”].text()
For this example, we get JSON as Request, the requirement is to get firstName and LastName from the JSON payload using JavaScript Expression
Below is my JSON Request Payload
{
"employees": {
"firstName": "John",
"lastName": "Doe"
}
}


For Retrieving firstName and lastName from JSON Request, below is my Java Script Expressions in the Request Stage



In the Response Stage, I just added a replace action to sending an XML Response

Testing
Request


Response


Checking the Request/Response Pipeline


Conclusion
The end-to-end JSON and JavaScript support are both powerful additions to Oracle SOA Suite 12c and using Javascript expression we can manipulate data without any overhead of XML Conversion. 

Please comment / leave a message with your inputs.