Fork me on GitHub

jsonlib 2.0.67

node:

XMLNode

THE
set_json()
FUNCTION
mixer
jsonlib 2.0.67
categories
Create
Modify
{
Overview

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.

*
This function has no arguments
Explore more ways to use this function in our Tritium Tester
»
Example
html("UTF-8") {
  $("/html/body") {
    jsonlib.hash() {
      jsonlib.key("type", "blog post")      
    }
  }
}
jsonlib.set_json()
»
View Source
node:

XMLNode

THE
append(Text %val)
FUNCTION
mixer
jsonlib 2.0.67
categories
Create
Modify
{
Overview

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

*
Argument
Type
Name
Text %val
Explore more ways to use this function in our Tritium Tester
»
Example
$("./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()"))
        }
      }
    }
  }
}
»
View Source
node:

XMLNode

THE
array()
FUNCTION
mixer
jsonlib 2.0.67
categories
Create
Modify
{
Overview

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

*
This function has no arguments
Explore more ways to use this function in our Tritium Tester
»
Example
$("./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()"))
        }
      }
    }
  }
}
»
View Source
node:

XMLNode

THE
hash()
FUNCTION
mixer
jsonlib 2.0.67
categories
Create
Modify
{
Overview

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.

*
This function has no arguments
Explore more ways to use this function in our Tritium Tester
»
Example
$("./body") {
  jsonlib.hash() {
    jsoblib.key("type", "blog post")
  }
}
»
View Source
node:

XMLNode

mixer
jsonlib 2.0.67
categories
Create
Modify
{
Overview

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.

*
Arguments
Type
Name
Text %key
Text %val
Explore more ways to use this function in our Tritium Tester
»
Example
$("./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")
      }
    }
  }
}
»
View Source