This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Using JSON with Data Services
The possibility of sending and receiving messages in JSON is enabled in the ESB of WSO2 Enterprise Integrator (WSO2 EI) by default. See the topics given below to understand how data can be exposed in JSON format, and how the data can be changed by sending JSON payloads.
Prerequisites
In this tutorial we will use a data service that exposes RDBMS data.
Upload the (already created) data service
You can download the RDBMSDataService from . If required, you can create the same data service from scratch following the tutorial on exposing an RDBMS as a data service.
By default, this data service exposes data in XML format. You can follow the steps in this tutorial to change the output type to JSON.
Create the RDBMS data store
If you haven't already completed the tutorial on exposing an RDBMS as a data service, follow the steps given below to set up the MySQL database for this tutorial.
Install the MySQL server.
Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/libdirectory.Create the following database:
EmployeesCreate the Employee table inside the Employees database:
CREATE TABLE Employees (EmployeeNumber int(11) NOT NULL, FirstName varchar(255) NOT NULL, LastName varchar(255) DEFAULT NULL, Email varchar(255) DEFAULT NULL, Salary varchar(255));
GET data using JSON
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
Log in to the product's management console and upload the data service.
Click Add → Data Service → Upload.
Browse and add the RDBMSDataService.dbs file.
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.
Shown below is a basic mapping.
Sample JSON Mapping
{ "Employees": {"Employee":[ {"EmployeeNumber":"$EmployeeNumber", "Details": { "FirstName":"$FirstName", "LastName":"$LastName", "Email":"$Email", "Salary":"$Salary" } } ] } }In a basic JSON output mapping, we specify the field values that we expect in the query result. You can give additional properties to this field mapping such as data type of the field, the possible content filtering user roles etc. These extended properties for the fields are given in parentheses, with a list of string tokens providing the additional properties, separated by a semicolon (";"). See the sample below.
Sample JSON Mapping
{ "Employees": {"Employee":[ {"EmployeeNumber":"$EmployeeNumber(type:integer)", "Details": { "FirstName":"$FirstName", "LastName":"$LastName", "Email":"$Email", "Salary":"$Salary(requiredRoles:hr,admin)" } } ] } }If you want to write a nested query using JSON, see the tutorial on working with nested queries.
Save the query.
Get data in JSON
The RDBMSDataService that you are using will already contain the following resource defined:
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 (http://curl.haxx.se). Shown below is the curl command to invoke the GET resource:
curl -X GET -H "Accept: application/json" http://localhost:8280/services/RDBMSDataService.HTTPEndpoint/Employee/1You 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 for updating employee information:
You can now 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 called employee-payload.json file, and define the JSON payload for posting new data as shown below.
{ "_postemployee": { "EmployeeNumber" : "14001", "LastName": "Smith", "FirstName": "Will", "Email": "will@google.com", "Salary": "15500.0" } }Send the following HTTP request from the location where the employee-payload.json file is stored:
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data "@employee-payload.json" http://localhost:8280/services/RDBMSDataService/Employee
Post data in batches
Batch requesting feature is enabled for the RDBMSDataService that you are using. This allows you to post JSON data in batches.
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.