String Operations in JSON Payloads

Image for post
Image for post

This article discusses about string operations can be done with json payloads using the WSO2 Data Mapper mediator.

Prerequisites

Follow the 1–4 steps in Development Guidelines Section in this post.

We are going to create String Operations API. So we are naming the API as StringOperations.xml.

Steps

  1. To the API Resource drag a Datamapper mediator and Respond mediator as below.
Image for post
Image for post

2. In this example we are going to concatenate two strings. Further we are using URI Template here. The reason for using a URI Template is we can add several API resources to the same API and it helps to implement different use cases through this approach. Make sure to do the following configurations to the API Resource.

Url Style: URI_TEMPLATE

URI-Template: /concat

Methods: POST:- true

3. Click on Datamapper Mediator. In Datamapper mediator properties, select the input type and output type as JSON since we are sending JSON payload.

Image for post
Image for post

4. Double click on Datamapper Mediator. It will open the datamapper diagram. For the concatenation operation below will be the datamapper diagram.

Use case: Concatenate employee’s initials and last name and create employee name in the output with employee age with no change.

Image for post
Image for post

5. Right click on Input type and upload the input file.

request.json

{
"Employees":
[
{
"initials":"[initials]",
"firstName":"[firstName]",
"lastName":"[lastName]",
"gender":"[gender]",
"age":"[age]"
},
{
"initials":"[initials]",
"firstName":"[firstName]",
"lastName":"[lastName]",
"gender":"[gender]",
"age":"[age]"
},
{
"initials":"[initials]",
"firstName":"[firstName]",
"lastName":"[lastName]",
"gender":"[gender]",
"age":"[age]"
}
]
}

response.json

{
"Employees":
[
{
"name":"[name]",
"gender":"[gender]"
},
{
"name":"[name]",
"gender":"[gender]"
},
{
"name":"[name]",
"gender":"[gender]"
}
]
}

6. Add all the configurations to the pom file of the composite application project, export the .car file and publish it to the WSO2 EI. (WSO2 EI server should be up and running)

Under the APIs section in WSO2 EI you can verify if API has been successfully deployed.

7. Now we can call the API as below.

POST /stringOperations/concat HTTP/1.1
Host: localhost:8280
Content-Type: application/json
cache-control: no-cache
Postman-Token: 0ce47c4a-9886-4cee-ace9-67264bc52efb
URL: http://localhost:8280/stringOperations/concat
{
"Employees":
[
{
"initials":"R.J.",
"firstName":"Leo",
"lastName":"Johnes",
"gender":"M",
"age":"29"
},
{
"initials":"I.M.",
"firstName":"Isuru",
"lastName":"Uyanage",
"gender":"F",
"age":"29"
},
{
"initials":"G.P.S.",
"firstName":"Supun",
"lastName":"De Silva",
"gender":"M",
"age":"31"
}
]
}

Response:

{
"Employees":
[
{
"name":"R.J.Jones",
"gender":"male"
},
{
"name":"I.M.Uyanage",
"gender":"F"
},
{
"name":"G.P.S.De Silva",
"gender":"M"
}
]
}

Likewise you can add more API resources to the same API with proper URI template such as /split, /case and etc.

Written by

Senior Software Engineer — QA at WSO2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store