JSON mapping support in data services is available as another result type in the output mappings section of the data services query. That is, you can specify JSON as the output type for the query and then provide the JSON template that should be used for service responses.
From DSS version 3.2.0 onwards, the default JSON message formatter/ builder is implemented based on
org.apache.axis2.json.gson, which supports some new features as explained below.
The following is a sample JSON mapping of the data services results. Here, we can see the JSON template, which is the format that is expected to be returned from our query.
As shown in the sample given above, the column name values that are expected in the query result should be referred to by the column name with the "$" prefix. E.g. "$customerName".
Also, the structure of the JSON template should follow some guidelines in order to be compatible with the result. These guidelines are:
- The top most item should be a JSON object. It cannot be a JSON array.
- For handling multiple records from the result set, the immediate child of the top most object can be a JSON array, and the array should contain only a single object.
- If only a single result is returned, the immediate child of the top most object can be a single JSON object.
- After the immediate child of the top most object, there cannot be other JSON arrays in the mapping.
All JSON responses will be returned as an array.
Extended Properties for Mapping Fields
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 semi colon (";").
The example given below shows how the 'data type' property is added to the 'age' field using parentheses "()".
- The extended property 'type' is given along with the value 'integer'.
- The property and the value are separated by a colon.
- Note that the possible values for data type are "integer", "long", "double" and "boolean".
Content Filtering (Required Roles)
The example given below shows how the 'data type' extended property as well as the 'required roles' extended property is added to the 'age' field using parentheses "()".
- The 'requiredRoles' property is added along with the values 'hr' and 'admin'.
- Note that the two values for required roles property are separated by a comma.
- Also, the two extended properties are separated by a semicolon.
Nested queries can be mentioned in the JSON mapping by giving the query details as a JSON object attribute. That is, the name of the target query to be called and the property value (the fields in the result mapped with the target query parameters) are included in the JSON mapping as the object attribute name. Example:
As shown above, the nested query mapping takes the following format:
Multiple parameters can be provided in a nested query as follows:
Note the following:
- The target query name is mentioned by prefixing the query name with "@". Note "@employeesInOfficeSQL" in the example given above.
- The parameter mapping is added to the query by giving the following values: The field name in the result prefixed by "$", and the name of the target query parameter. These two values in the parameter mapping are separated by "->".
- Note that the target query name and the parameter mapping are separated by a colon as follows: "@employeesInOfficeSQL": "$officeCode->officeCode"
A JSON Mapping Sample in WSO2 Data Services Server can be found here: JSON Mapping Sample.
JSON Mapping in tenant mode
To run this feature in tenant mode, remove the following content in the
Then, uncomment the following content in the same files.