Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Log in to the product's management console and upload the data service.
    1. Click Add → Data Service → Upload.
    2. Browse and add the RDBMSDataService.dbs file.
  2. Click List under Main → ServicesThe RDBMS data service should be listed.
  3. Click the data service to open the Service Dashboard.
  4. Click Edit Data Service (Wizard) to open the data service using the Create Data Service wizard.
  5. Click Next until you get to the Queries screen.
  6. Edit the GetEmployeeDetails query.
  7. Change the Output Type to JSON
  8. 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.

      Code Block
      languagejavascript
      titleSample JSON Mapping
      { "Employees":
            {"Employee":[
              {"EmployeeNumber":"$EmployeeNumber",                        
               "Details": {
                "FirstName":"$FirstName",
                "LastName":"$LastName",
                "Email":"$Email",
                "Salary":"$Salary"
               }
              }                  
            ]
          }            
      } 
      Tip

      Note the following:

      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. "$EmployeeNumber".

      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.

    • 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.

      Code Block
      languagejavascript
      titleSample JSON Mapping
      { "Employees":
            {"Employee":[
              {"EmployeeNumber":"$EmployeeNumber(type:integer)",                        
               "Details": {
                "FirstName":"$FirstName",
                "LastName":"$LastName",
                "Email":"$Email",
                "Salary":"$Salary(requiredRoles:hr,admin)"
               }
              }                  
            ]
          }            
      }
      Tip
      Setting data types
      • The 'data type' property is added to the 'EmployeeNumber' 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 'data type' extended property, as well as the 'required roles' extended property, is added to the 'Salary' field using parentheses "()".
      • The 'requiredRoles' property is added along with the values 'hr' and 'admin'. 
      • Note that the two values for the 'requiredRoles' property are separated by a comma. 
      • Also, the two extended properties are separated by a semicolon. 
    • If you want to write a nested query using JSON, see the tutorial on working with nested queries.

  9. Save the query.

Get data in JSON

The RDBMSDataService that you are using will already contain the following resource defined:

...

Tip
titleCreating 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

However, if the RESOURCE_PATH specified in the data service contains the "/" symbol, be sure to replace the "/" symbol 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. For example, if the RESOURCE_PATH

Note

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

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.If you are sending a batch request to the "/employee/add" resource, the payload object name should be as follows:

Code Block
{
    "_post_employee_add_batch_req": {
        "_post_employee_add": [
            {
               ..........
            },
            {
               ..........
            }
        ]
    }
}