Appendix: Using the GENERATEJSON Element Tag
The GENERATEJSON element creates bespoke, JSON-formatted data and assigns it to a local keyword.
Implementation:
- Click to open the Element Browser to the right of the window.
- Use the Filter menu to select Tags.
- Locate and select GENERATEJSON.
The element has the following syntax:
<%# GENERATEJSON(
Keyword
[Data]) %>
The following parameters are required:
Parameter | Description |
---|---|
Keyword | The keyword of the variable that will store the new data. |
[Data] | One or more key-value pairs that define the structure and content of the JSON data. The order of these parameters determines the order of the resulting data. |
The Keyword parameter can appear at any point in the element definition. You can include a second Keyword parameter if you want the resulting JSON to contain an item named Keyword, however the element will use the first occurrence to determine where to save the resulting data. For that reason, using Keyword more than once is not recommended.
The simplest type of Data parameter consists of a paired item name and value. The following example contains two such pairs:
<%# GENERATEJSON(
Keyword="MyJSON"
Item1Name="Item1Value"
Item2Name="Item2Value") %>
This example results in the following JSON data being assigned to the MyJSON keyword:
{ "Item1Name":"Item1Value", "Item2Name":"Item2Value" }
Any pair in the element's Data set can be assigned one of the following special values:
- [true] or [false] - Boolean True or False.
- [null] - a Null value.
As opposed to assigning the literal strings "true", "false", or "null" to an item, these special terms will assign a value with no quote marks.
- [group] - designate the item as a JSON Object, allowing subsequent items to be added as members of that object.
The syntax needed to add an item to an object named MyObject is "MyObject.ItemName=ItemValue". You can nest one object within another by defining a second group as one of the object's items, e.g. "MyObject1.MyObject2=[group]". You can use this syntax to create as many nested levels as you need.
- [list] - designate the item as a JSON Array, allowing subsequent items to be added as members of that array.
The syntax needed to add an item to an array named MyArray is "MyArray=MyArrayItemValue". If you assign [list] to the same item twice, a second array will be added to the first as a new member. To assign values to objects or arrays that are themselves members of an array, you will need to provide the full contextual path, including array index numbers where needed. For example, to add a new member to Array2, which itself is the first member of Array1, you would need to use the syntax "Array1[0]=ItemValue".
The following example combines standard key-value pairs and all available special values to create a short, but moderately complex demonstration of the GENERATEJSON element's capabilities:
<%# GENERATEJSON(
Keyword="ContactList"
Owner="Jim Goodby"
Private="[false]"
Password="[null]"
Contacts="[list]"
Contacts="[group]"
Contacts[0].Name="Chris Breeze"
Contacts[0].HomeAddress="[group]"
Contacts[0].HomeAddress.Number="12"
Contacts[0].HomeAddress.Street="Bolan Avenue"
Contacts[0].HomeAddress.City="Tappleton"
Contacts[0].HomeAddress.County="[null]"
Contacts[0].HomeAddress.PostCode="TP8 0QQ"
Contacts[0].HomeAddress.Country="United Kingdom"
Contacts[0].Mobile="07778888888"
Contacts[0].IsFavourite="[true]") %>
After this element is processed, the data assigned to ContactList is as follows:
{ "Owner":"Jim Goodby", "Private":false, "Password":null, "Contacts":[ { "Name":"Chris Breeze", "HomeAddress":{ "Number":"12", "Street":"Bolan Avenue", "City":"Tappleton", "County":null, "PostCode":"TP8 0QQ", "Country":"United Kingdom" }, "Mobile":"07778888888", "IsFavourite":true } ] }
In addition to basic headline information such as the contact list's Owner, this data contains the Contacts array, which can hold as many contacts as needed. In this example, there is only one contact (Chris Breeze, identified in the element as Contacts[0]).
Like all local JSON data, you can modify the output of a GENERATEJSON element using the ParseJSON function.