Primitives

In this tutorial we will go over how to add key names and their associated primitive type values to a JavaScriptObjectNotation object, including: 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

This code will output the name and integer value pairs in JavaScript Object Notation format

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

This code will output the name and number value pairs in JavaScript Object Notation format

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

This code will output the name and boolean value pairs in JavaScript Object Notation format

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

This code will output the name and text value pairs in JavaScript Object Notation format

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.