Tutorial: Adding Primitive Values to a JavaScriptObjectNotation Object

This tutorial tells you how to add primitive values to a JavaScriptObjectNotation (JSON) object

Primitives

In this tutorial we will go over how to add key names and their associated primitive type values to a JavaScriptObjectNotation object, including:
  • Integer Values : such as (1, 2, 3)
  • Number Values : such as (1.0, 2.2, 3.7)
  • Boolean Values : true or false
  • Text Values : such as ("name", "John", "Smith")
Although integer value types are not standardly used in JavaScript Object Notation, we have added the integer value type as an added convenience for the user.

Adding an Integer Value to a JavaScriptObjectNotation Object

We start by creating a JavaScriptObjectNotation object, called “json” in this example.

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
    action Main
        JavaScriptObjectNotation json

We then have to “Add” the text key, and associated integer value to the JavaScriptObjectNotation object json.

         json:Add("item1", 1)
         json:Add("item2", 2)

Here is the complete code section, which outputs our formatted JavaScriptObjectNotation object to the screen, allowing us to view each of the { name : value } pairs within the curly braces that encapsulate the object:

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
    action Main
        JavaScriptObjectNotation json
        json:Add("item1", 1)
        json:Add("item2", 2)
        output json:ToText()
    end
end

Where the output should look something like this:

{
  "item1": 1,
  "item2": 2
}

Adding Other Value Types to a JavaScriptObjectNotation Object

Similar to the way in which we added an integer value to the JavaScriptObjectNotation we start by creating a JavaScriptObjectNotation object. However, when “Add”ing our { name : value } pairs to the object we will change the value type accordingly to a number, boolean, or text type.

Adding a number type value to the JavaScriptObjectNotation object:

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
    action Main
        JavaScriptObjectNotation json
        json:Add("item1", 1.1)
        json:Add("item2", 2.2)
        output json:ToText()
    end
end

Adding a boolean type value to a JavaScriptObjectNotation object:

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
    action Main
        JavaScriptObjectNotation json
        json:Add("item1", true)
        json:Add("item2", false)
        output json:ToText()
    end
end

Adding a text type value to a JavaScriptObjectNotation object:

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
    action Main
        JavaScriptObjectNotation json
        json:Add("firstName", "Joe")
        json:Add("lastName", "Smith")
        output json:ToText()
    end
end

Next Tutorial

In the next tutorial, we will discuss JavaScript Object Notation Using Nesting, which describes how to nest a JavaScriptObjectNotation object as a value within another JavaScriptObjectNotation object.