jsonlib 2.0.67
Final call to set your final JSON object.
The set_json function is the final function to call once your JSON structure has been arranged. The function must be called outside the html() scope - usually in your main.ts file.
Common Uses
- "JSON-ifying" content using Moovweb.
In the example below, we set our final JSON object after all the transformations have been applied.
html("UTF-8") { $("/html/body") { jsonlib.hash() { jsonlib.key("type", "blog post") } } } jsonlib.set_json()
Allows you to append content into your arrays.
The append()
function is used to add text, objects, or arrays into your arrays.
When passed a value parameter, that text is added to the existing array being created.
Without a parameter, you may open the append()
scope to add things like objects or new arrays into your existing array.
Note the append function only works inside the array()
scope.
Common Uses
- Adding content to your arrays in JSON responses.
In the example below, we create an array. The first value has a string with the text "random comment". The following values are all objects with the keys user, data, and content. The number of objects depends on the number of elements selected with the class="comment".
$("./body") { jsonlib.array() { jsonlib.append("random comment!") $(".//div[@class='comment']") { jsonlib.append() { jsonlib.hash() { jsonlib.key("user", fetch(".//div[@class='c-user']/text()")) jsonlib.key("date", fetch(".//div[@class='c-date']/text()")) jsonlib.key("content", fetch(".//div[@class='c-content']/text()")) } } } } }
Create arrays in your JSON object.
The array()
function allows you to insert arrays into your JSON objects.
You must always use the append()
function inside the array()
scope to either insert a string or a new hash/object into the array.
Note that it often makes sense to use Tritium selectors to iteratively assign values inside arrays. Then inside the selectors, you use the append()
function to insert array values.
Note you can only have one root scope to your JSON response and it must be either array()
or hash()
. Any following calls to array()
or hash()
that aren't nested inside that scope will be ignored.
Common Uses
- Creating arrays in your JSON response.
In the example below, we create an array. The first value has a string with the text "random comment". The following values are all objects with the keys user, data, and content. The number of objects depends on the number of elements selected with the class="comment".
$("./body") { jsonlib.array() { jsonlib.append("random comment!") $(".//div[@class='comment']") { jsonlib.append() { jsonlib.hash() { jsonlib.key("user", fetch(".//div[@class='c-user']/text()")) jsonlib.key("date", fetch(".//div[@class='c-date']/text()")) jsonlib.key("content", fetch(".//div[@class='c-content']/text()")) } } } } }
Opens the hash scope creating a new JavaScript object.
The hash()
function is used to create new objects. Inside the hash()
scope you can assign keys.
Note you can only have one root scope to your JSON response and it must be either array()
or hash()
. Any following calls to array()
or hash()
that aren't nested inside that scope will be ignored.
Common Uses
- Creating objects in your JSON response.
In the example below, we open the hash scope and set our first key.
$("./body") { jsonlib.hash() { jsoblib.key("type", "blog post") } }
Assign a key/value pair, or create a key and scope into it.
The key function allows you to assign keys and their corresponding values. You can also scope into keys to add new objects inside them. The name is the first argument passed, the optional value parameter is the second.
Common Uses
- Creating keys inside your JSON objects.
In the example below, we open the hash scope, set several key values, and then open the "comments" key scope and insert a new object with two comments.
$("./body") { jsonlib.hash() { jsoblib.key("type", "blog post") jsonlib.key("title", fetch(".//h1[@id='title']/text()")) jsonlib.key("content", fetch(".//div[@id='content']/text()")) jsonlib.key("comments") { jsonlib.hash() { jsonlib.key("1", "first comment") jsonlib.key("2", "second comment") } } } }