Using JSON with Data Services
You can send and receive JSON messages by default via WSO2 Enterprise Integrator's (WSO2 EI) ESB profile. See the topics given below to understand how data can be exposed in the JSON format, and how data can be changed by sending JSON payloads. In this tutorial, you will use a data service that exposes RDBMS data.
Before you begin!
If you have not tried the Exposing a Datasource as a Data Service tutorial previously follow the steps given below:
- Download the RDBMSDataService from here.
- Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory. This is located in a place specific to your OS as shown below:OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.4.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.4.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.4.0
CentOS /usr/lib64/EnterpriseIntegrator/6.4.0
Start the WSO2 ESB profile.
- Go to product's management console:
https://localhost:9443/carbon
- Enter
admin
as the username and password. - Click Add > Data Service > Upload.
- Browse and add the
RDBMSDataService.dbs
file you downloaded.
A data service can expose data in one of the following formats: XML, RDF, or JSON. You can select the required format by specifying the output type for the data service query. To expose data in JSON, you need to select JSON as the output type, and map the output to a JSON template.
Map the output type to JSON
The data service we uploaded or created previously, maps the output type to XML. Follow the steps given below to change it to JSON.
Open the ESB profile's Management Console using
https://localhost:9443/carbon
, and log in usingadmin
as the username and the password.- Click List under Main > Services. The RDBMS data service should be listed.
- Click the data service to open the Service Dashboard.
- Click Edit Data Service (Wizard) to open the data service using the Create Data Service wizard.
- Click Next until you get to the Queries screen.
- Edit the GetEmployeeDetails query.
- Change the Output Type to JSON.
You can now start defining the JSON template for the output. Listed below are a few sample templates that you can use for this query.
- Save the query.
GET data in JSON
The RDBMSDataService that you are using already contains the following resource:
Resource Path | Employee/{EmployeeNumber} |
---|---|
Resource Method | GET |
Query ID | GetEmployeeDetails |
You can now RESTfully invoke the above resource. To send a JSON message to a RESTful resource, you can simply add the “Accept
:Application/json
” to the request header when you send the request. The service can be invoked in REST-style via curl.
Shown below is the curl command to invoke the GET resource:
curl -X GET -H "Accept: application/json" http://localhost:8280/services/RDBMSDataService/Employee/{EmployeeNumber}
Example:
curl -X GET -H "Accept: application/json" http://localhost:8280/services/RDBMSDataService/Employee/1
You will receive the response in JSON format.
POST/UPDATE data using JSON
When a client sends a request to change data (POST/PUT/DELETE) in the datasource, the HTTP header Accept
should be set to application/json
. Also, if the data is sent as a JSON payload, the HTTP header Content-Type
should be set to application/json
.
The RDBMSDataService that you are using will already contain the following resources for adding and updating data.
Resource for adding employee information:
Resource Path Employee
Resource Method POST
Query ID AddEmployeeDetails
Resource for updating employee information:
Resource Path Employee
Resource Method PUT
Query ID UpdateEmployeeDetails
You can RESTfully invoke the above resource by sending HTTP requests as explained below.
Post data
To post new employee information, you need to invoke the resource with the POST method.
First, create a file named
employee-payload.json
, and define the JSON payload for posting new data as shown below.The following payload works for this use case. When you create payloads for different use cases, be mindful of the tips given here.
{ "_postemployee": { "EmployeeNumber" : "14001", "LastName": "Smith", "FirstName": "Will", "Email": "will@google.com", "Salary": "15500.0" } }
On the terminal, navigate to the location where the
employee-payload.json
file is stored, and execute the following HTTP request:curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data "@employee-payload.json" -k -v http://localhost:8280/services/RDBMSDataService/Employee
Post data in batches
You are able to post JSON data in batches using the RDBMSDataService that you created or uploaded.
To verify that batch requesting is enabled:
- Log in to the product's management console.
- Click List under Main > Services and select the RDBMSDataService.
- Click the data service to open the Service Dashboard.
- Click Edit Data Service (Wizard) to open the data service using the Create Data Service wizard.
- See that the Batch Requesting check box is selected.
First, create a file named
employee-batch-payload.json
, and define the JSON payload for posting multiple employee records (batch) as shown below.The following payload works for this use case. When you create payloads for different use cases, be mindful of the tips given here.
{ "_postemployee_batch_req": { "_postemployee": [ { "EmployeeNumber": "5012", "FirstName": "Will", "LastName": "Smith", "Email": "will@smith.com", "Salary": "13500.0" }, { "EmployeeNumber": "5013", "FirstName": "Parker", "LastName": "Peter", "Email": "peter@parker.com", "Salary": "15500.0" } ] } }
On the terminal, navigate to the location where the
employee-batch-payload.json
file is stored, and execute the following HTTP request:curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data "@employee-batch-payload.json" -k -v http://localhost:8280/services/RDBMSDataService/Employee_batch_req
Update data
To update the existing employee records, you need to invoke the resource with the PUT method.
First, create a file named
employee-upload-update.json
, and define the JSON payload for updating an existing employee record as shown below.
For example, change the salary amount. Make sure that the employee number already exists in the database.The following payload works for this use case. When you create payloads for different use cases, be mindful of the tips given here.
{ "_putemployee": { "EmployeeNumber" : "1", "FirstName": "Will", "LastName": "Smith", "Email": "will@smith.com", "Salary": "78500.0" } }
On the terminal, navigate to the location where the
employee-upload-update.json
file is stored, and execute the following HTTP request:curl -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' --data "@employee-upload-update.json" -k -v http://localhost:8280/services/RDBMSDataService/Employee
Creating JSON payloads
Note the following when you define a JSON payload: The object name specified in the payload must be in the following format: "_<HTTP_METHOD><RESOURCE_PATH>
" where RESOURCE_PATH
represents the path value specified in the data service resource. For example, if the RESOURCE_PATH
is "employee", the payload object name should be as follows:
- For HTTP POST requests:
_postemployee
- For HTTP PUT requests:
_putemployee
The child name/values of the child fields in the payload should be the names and values of the input parameters in the target query.
Handling a resource path with the "/" symbol
If the RESOURCE_PATH
specified in the data service contains the "/" symbol, be sure to replace the "/" symbol with the underscore symbol ("_") in the payload object name.
Important! In this scenario, the RESOURCE_PATH
value should only contain simple letters. For example, the value can be "/employee/add"
but not "/Employee/Add"
.
For example, if the RESOURCE_PATH
is /employee/add
, the payload object name should be as follows:
- For HTTP POST requests:
_post_employee_add
- For HTTP PUT requests:
_put_employee_add
If you are sending a batch request to the "/employee/add
" resource, the payload object name should be as follows:
{ "_post_employee_add_batch_req": { "_post_employee_add": [ { .......... }, { .......... } ] } }