Writing JavaScript Object Notation Formatted Data as a Text Value or to a File

This tutorial will teach you how to write JavaScript Object Notation (JSON) formatted data as a text value or to a file

Writing JavaScript Object Notation Objects as a Text Value

To write formatted JavaScript Object Notation data as a text value we will use the “ToText()” action on the JavaSciptObjectNotation object which will output the associated JavaScriptObjectNotation object to the screen by default. Using just the "ToText()" action on its own will output the JavaScriptObjectNotation object as a single line of text with the {name : value} pairs separated only by a comma. To output the JavaScriptObjectNotation with the {name : value} pairs separated by a comma and a carriage return, we must also use the "SetPrettyPrint()" action with the boolean parameter set to true. In the following example we begin by creating a JavaScriptObjectNotation object “json” and adding a few { name : value } pairs to the object. To write ”json” as a text value in JavaScript Object Notation format we will first set "json" to use PrettyPrint and then output the JavaScriptObjectNotation text value to the screen

Here is the complete code to output the JavaScriptObjectNotation object “json” as a text value to the screen:

use Libraries.Data.Formats.JavaScriptObjectNotation

class Main
   action Main
       JavaScriptObjectNotation json
       json:Add("item1", 1)
       json:Add("item2", 2.2)
       json:Add("item3", "three")
       json:SetPrettyPrint(true)
       output json:ToText()
   end
end

The text value output to the screen should look similar to this:

{
       "item1": 1,
       "item2": 2.2,
       "item3": "three"
}

Try changing the "SetPrettyPrint()" boolean parameter to false and you will notice the change in format.

Writing JavaScript Object Notation Objects to a File

In order to write a JavaScripObjectNotation object to a file we must use the additional “Libraries.System.File” library. After adding our { name : value } pairs to the JavaScriptObjectNotaion object “json” we will create a File object and specify the path for the File called "myFile" in this example. In specifying the path for "myFile" we can either create a new file path or rewrite to an existing file path. For this example we will use the "response.json" file path that we read in from in the last tutorial. By writing to an existing file, we will replace the contents of the existing file with the JavaScriptObjectNotation object "json" that we are creating in this example. We will Write to the file using the “json:ToText()” action within the “Write()” action. Finally, note that we can only write to a File when running Quorum offline because of security on the Internet.

Here is the complete code to write a JavaScriptObjectNotation object to a file:

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.System.File

class Main
   action Main
       JavaScriptObjectNotation json
       json:Add("item1", 1)
       json:Add("item2", 2.2)
       json:Add("item3", "three")
       json:SetPrettyPrint(true)
        //Creates a File object called “myFile” Sets the Path for “myFile”
        //(where you will be writing the JavaScriptObjectNotation object “json” to,
        //in this example we are using the file path for our downloaded "response.json" file)
        //and Writes the JavaScript Object Notation formatted “json” 
        //to the specified file path.
       File myFile
       myFile:SetWorkingDirectory("/Users/userName/Downloads")
       myFile:SetPath("response.json")
       myFile:Write(json:ToText())
   end
end

Your "response.json" file will now contain the output from this example which will look like this:

{
       "item1": 1,
       "item2": 2.2,
       "item3": "three"
}

Writing a JavaScriptObjectNotation Object as a List with Name/Value Pairs

In order to get a list (array) of JavaScriptObjectNotation object key name and value pairs we must use the “Libraries.Containers.Array” library. In this example we will output a list of key names followed by an arrow and the name’s value.

use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Containers.Array

class Main
   action Main
       JavaScriptObjectNotation json
       json:Add("item1", 1)
       json:Add("item2", 2.2)
       json:Add("item3", true)
       json:Add("item4", "text")
        //Loop to output a list of each key and the key’s associated value in the
        //JavaScriptObjectNotation object “json”
       Array<text> keys = json:GetKeys()
       i = 0
       repeat keys:GetSize() times
           key = keys:Get(i)
           output key + " => " + json:GetValue(key)
           i = i + 1
       end
   end
end

This example should produce a list of all of “json’s” name value pairs in the format (name => value). The output should look similar to this:

item1 => 1
item2 => 2.2
item3 => true
item4 => text

Next Tutorial

In the next tutorial, we will discuss JavaScript Object Notation key names and values, which describes how to return key names and values from JavaScriptObjectNotation objects.