JSONDataSource
Attempts to generate a string of JSON data based on the results of a data source query.
Syntax
JSONDataSource( DataSourceName, FilterName, Refresh, ModelReference );
Parameters
Parameter Name | Type | Description | Optional? | Default Value |
---|---|---|---|---|
DataSourceName | String | The name of the data source to be queried. | No | n/a |
FilterName | String | The name of a filter associated with the data source. Only data that satisfies this filter will be included in the result of the function call. | Yes | "" (no filter) |
Refresh | Boolean | Determines whether the function will refresh the data source before executing a query. The data source will only be refreshed once per postback. | Yes | 0 |
ModelReference | String | The model reference associated with the chosen data source. If the function is called by a model, it assumes the data source exists within that model, so you do not need to specify a reference unless the data source exists in a different model on the same repository; if the function is called outside the context of a model - for example, in a web page expression or via the DATASOURCE element tag - you must specify the model containing the data source. | Yes (see Description) | "" |
Output
If the function is successful, it will return a string consisting of the generated JSON data. If the query fails - for example, because the specified data source or filter does not exist - the function will return a value of False.
The data returned by the query will form an array named results.
Use Case
In the following example, the CatalogueService model contains a data source called Products. After applying a filter called OutOfStock, querying the data source returns the following:
ID | Description | InStock |
---|---|---|
0086 | 30ft rope | False |
0194 | Medium padlock | False |
0358 | Binoculars, black | False |
For the model to generate a string of JSON based on this set of results, you would need the following expression:
JSONDataSource( "Products", "OutOfStock" );
The results of this expression are as follows:
{"status": 0, "statusMessage": "Success", "result": [{"ID":"0086","Description":"30ft rope","InStock":"False"},{"ID":"0194","Description":"Medium padlock","InStock":"False"},{"ID":"0358","Description":"Binoculars, black","InStock":"False"}]}
Since the expression was called by the model that contains the data source, there was no need to specify a model reference. However, if you wanted to call the expression outside of the model - such as within a different model, or as part of a web page's markup - you would need to modify the expression as follows:
JSONDataSource( "Products", "OutOfStock", "", "CatalogueService" );
This expression uses the function's fourth parameter, so the third parameter also needs a value. In this example there's no need to refresh the data source, so an empty string was used to instruct the expression to use the default value - false. If you do want to refresh the data before generating the JSON string, one last modification is necessary:
JSONDataSource( "Products", "OutOfStock", 1, "CatalogueService" );
Since this expression references the CatalogueService model, it can be called anywhere on the same repository as that model.