<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=2233467260228916&amp;ev=PageView&amp;noscript=1">

Retrieve data from JSON Using JavaScript expressions in Oracle Service Bus 12.2.1.2

Cegal Tech Advisors Fighting fuzz by striving to make your coding life more straight forward.
02/17/2019 |

SOA Suite with Service Bus 12.2.1.2 provides a JavaScript action, which can be used in OSB pipelines. 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. Now, you can use JSON objects directly by using the JavaScript activity. So, rather than converting the payload to XML and using XQuery or XSLT for mapping the data here, we can use JavaScript directly to manipulate the JSON object. 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.

TIP: The JavaScript action is available for any pipeline type, not just Native REST pipelines.

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()

Demo

 

In this demo example, I will demonstrate how to use JSON as a Request with an emphasis on fetching the JSON payload using JavaScript activity.

First, we will create a simple HTTP Transport with REST as a request service, which we can do like in the next pictures following the create wizard.

And this should result in the project Design view.

Next, we will open the pipeline view and create a Pipeline Pair in which we have Request and Response pipelines. In the Request pipeline we will use JavaScript activity to retrieve the JSON payload data. In the Response pipeline, a Replace activity for mapping the JSON data to an XML message is an example.

Below is our JSON Request Payload:

{
"employees": {
"firstName": "John",
"lastName": "Doe"
}
}

For Retrieving firstName and lastName from JSON Request, below are JavaScript Expressions in the Request Stage:

In the Response Stage, in a replace action we will create a simple mapping for an XML Response.

Testing

Now, we can set to deploy our integration and do a first test. In the Service Bus Test console select Media Type: application/JSON and paste the JSON payload as in the picture below.

This should be a result of our test:

I hope this was helpful! 💚

 

Related articles

Call REST API in Service Bus 12.2.1.2 (JSON Request/Response)
Cegal Tech Advisors Fighting fuzz by striving to make your...
arrow
Handle custom JSON body for HTTP API with OSB 12.2.1
Cegal Tech Advisors Fighting fuzz by striving to make your...
arrow
Oracle
Solving the OSB 12c Oracle data source installation error for OCI
Cegal Tech Advisors Fighting fuzz by striving to make your...
arrow