Tutorial: Returning Key Names and Values from a JavaScriptObjectNotation Object
This tutorial tells you how to return a list of JavaScriptObjectNotation object key names and their associated valuesReturning a List of Key Names from a JavaScriptObjectNotation Object
In order to return a list of all the key names included in a JavaScriptObjectNotation object we will use an Array container to manage the key names and then output those key names to the screen. We will start by adding the necessary libraries and creating a JavaScriptObjectNotation object “json” with nested JavaScriptObjectNotation object values.
use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Containers.Array
class Main
action Main
JavaScriptObjectNotation json
JavaScriptObjectNotation joe
JavaScriptObjectNotation jill
joe:Add("First Name", "Joe")
joe:Add("Last Name", "Smith")
joe:Add("Age", 15)
joe:Add("GPA", 3.89)
jill:Add("First Name", "Jill")
jill:Add("Last Name", "Miller")
jill:Add("Age", 14)
jill:Add("GPA", 3.65)
json:Add("Student1", joe)
json:Add("Student2", jill)
json:Add("School", "Elm Creek High School")
json:Add("City", "Jackson")
json:Add("State", "MS")
json:Add("Zip Code", 12345)
Now that we have a JavaScriptObjectNotation object "json" to gather key names from, we will create an Array of text values called “keys,” and set that Array to collect all of the key names in our JavaScriptObjectNotation object “json” by using the “GetKeys()” action.
//creates the Array of text values "keys"and assigns "keys" to the text values of the
//key names in out JavaScriptObjectNotation object "json"
Array<text> keys = json:GetKeys()
//loop to continue "Get"ting "key's" key names one at a time and output each key
//name until there are no more key names to "Get"
i = 0
repeat keys:GetSize() times
key = keys:Get(i)
output key
i = i + 1
end
The entire code section should look like this:
use Libraries.Data.Formats.JavaScriptObjectNotation
use Libraries.Containers.Array
class Main
action Main
JavaScriptObjectNotation json
JavaScriptObjectNotation joe
JavaScriptObjectNotation jill
joe:Add("First Name", "Joe")
joe:Add("Last Name", "Smith")
joe:Add("Age", 15)
joe:Add("GPA", 3.89)
jill:Add("First Name", "Jill")
jill:Add("Last Name", "Miller")
jill:Add("Age", 14)
jill:Add("GPA", 3.65)
json:Add("Student1", joe)
json:Add("Student2", jill)
json:Add("School", "Elm Creek High School")
json:Add("City", "Jackson")
json:Add("State", "MS")
json:Add("Zip Code", 12345)
Array<text> keys = json:GetKeys()
i = 0
repeat keys:GetSize() times
key = keys:Get(i)
output key
i = i + 1
end
end
end
The output should look similar to this:
Student1
Student2
School
City
State
Zip Code
Notice how there is no indication that “Student1” and “Student2” contain nested JavaScriptObjectNotation object values. All that we have returned to us are the key names connected to the values, which can be very helpful when trying to sort through large amounts of data for relevant information. In the next section we will go over how to return the values associated with specific key names in a JavaScriptObjectNotation object.
Returning Values from a JavaScriptObjectNotation Object
Once you have a list of all the key names in a JavaScriptObjectNotation object, you can use those key names to determine which specific key name values you would like to have returned for viewing, rather than having all of the key name and value pairs returned to you at once. In order to return a specific key name’s value we will use the “GetValue()” action which takes one text type value as a parameter. This parameter should be the key name associated with the value(s) that you wish to have returned. In the example below, we will use the JavaScriptObjectNotation object “json” with the nested JavaScriptObjectNotation object values “joe” and “jill” that we used in the previous tutorial. We will be using the list of key names that was returned to us to determine which of their values we would like to “Get”.
In order to specify which value(s) we wish to have returned, we will pass in the value's key name as a parameter to the “GetValue()” action. In this example we are going to have the “School” value and the “Student1” value returned to us. Since “Student1” has a nested JavaScriptObjectNotation object as its value, the “GetValue()” action will return the entire JavaScriptObjectNotation object “joe” value. You can add the following lines of code to your existing code section from the previous tutorial.
output "Student1: " + json:GetValue("Student1")
output "School: " + json:GetValue("School")
The complete code section to create a Nested JavaScriptObjectNotation object and return specific values located within that JavaScriptObjectNotation object looks like this:
use Libraries.Data.Formats.JavaScriptObjectNotation
class Main
action Main
JavaScriptObjectNotation json
JavaScriptObjectNotation joe
JavaScriptObjectNotation jill
joe:Add("First Name", "Joe")
joe:Add("Last Name", "Smith")
joe:Add("Age", 15)
joe:Add("GPA", 3.89)
jill:Add("First Name", "Jill")
jill:Add("Last Name", "Miller")
jill:Add("Age", 14)
jill:Add("GPA", 3.65)
json:Add("Student1", joe)
json:Add("Student2", jill)
json:Add("School", "Elm Creek High School")
json:Add("City", "Jackson")
json:Add("State", "MS")
json:Add("Zip Code", 12345)
output "Student1: " + json:GetValue("Student1")
output "School: " + json:GetValue("School")
end
end
The output should look similar to this:
Student1: {"First Name": "Joe", "Last Name": "Smith", "Age": 15, "GPA": 3.89}
School: Elm Creek High School
End of Lesson
You have reached the end of the lesson for Reading and Writing JavaScript Object Notation (JSON) Data. To view more tutorials, press the button below or you can go back to the previous lesson pages.