View Templates Configuration
Sunbird RC provides a functionality to transform the entity response body with different transformation templates
Last updated
Was this helpful?
Sunbird RC provides a functionality to transform the entity response body with different transformation templates
Last updated
Was this helpful?
In SunbirdRC we can configure view templates (JSON transformers) which can be applied during runtime. It supports enabling/disabling properties in JSON responses. It also provides executing simple expressions and also supports creating custom provider functions.
Below is an example of a view template:
When the above template is applied for the below response payload:
The response will be transformed to:
The view templates support configuring what fields should be displayed, changing the field names, and also performing functionalities on top of the fields.
In the above template, a field called firstName
which is present in the entity is been renamed to NAME.
By default, all fields defined in the template are treated to be "display": true
. If the fields are not supposed to be displayed then we can set "display": false
We can also define expressions or custom functions to perform complex/custom transformations.
In the above example, a custom JEXL expression to concatenate two fields is inline defined in the view template.
The above code illustrates on how to call the inline-defined functions for a specific field.
We can also write Java code which can be used to perform custom/complex operations and also execute that in the view template.
A custom provider function should implement IViewFunctionProvider
The above template will transform the below response payload
to the below format
The view templates can be applied to both the GET APIs. We need to pass a viewTemplateId
the header which contains the value of the view template file to be applied.
Example:
View templates can be used in search/discovery APIs. The respective template id needs to be part of the request body as below
The implementation of the above provider is here,
Similarly, SunbirdRC is shipped with another provider functions that can be used for transformation, The above provider function will remove JSON paths from a nested object. Example to demonstrate how to use the above function,