Skip to main content

NotesJSONNavigator (LotusScript)

LotusScript class used to parse JSON data.

Containment

Contained by NotesSession.

Properties

PreferUTF8

Methods

GetElementByName

GetElementByPointer

GetFirstElement

GetNextElement

GetNthElement

Stringify

AppendElement

AppendArray

AppendObject

Creation

To create a new NotesJSONNavigator object, use createNotesJSONNavigator.

Available options are no input, string, or NotesStream:

No input:

Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
Set jsonNav = session.Createjsonnavigator("")

String as input:

Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
set jsonNav = session.CreateJSONNavigator("")

NotesStream class as input:

Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
set jsonNav = session.CreateJSONNavigator(NotesStream)

Note: The NotesStream must be opened when creating the navigator and can be closed as soon as the navigator is created. Only UTF8 data is supported.

Syntax

Given a session, the following example creates a NotesJSONNavigator with the provided JSON string:

Dim jsnav As NotesJSONNavigator
Set jsnav = session.CreateJSONNavigator(|{"test":"A string"}|)

By default, the NotesJSONNavigator represents a JSON object as the root element. To set the type of the root element to a JSON array, create the NotesJSONNavigator as follows:

Dim jsnav as NotesJSONNavigator
Set jsnav = session.CreateJSONNavigator("[]")

Example

The following code provides examples of how to create and manipulate JSON objects in LotusScript using the NotesJSONNavigator class, as well as how to navigate within a JSON object to retrieve specific elements.

Example-1:

Option Public
Option Declare

Sub Initialize
End Sub

%REM
Sub SampleAppend
Description: creates this JSON object:
{
"variable1" : "value1",
"variable2" : "value2",
"numericVariable", 123
}
%END REM
Sub SampleAppendElement()
Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator

'# create empty navigator object
Set jsonNav = session.Createjsonnavigator("")

'# append name/value pairs
Call jsonNav.appendElement("value1", "variable1")
Call jsonNav.appendElement("value2", "variable2" )
Call jsonNav.appendElement(123, "numericVariable" )

'# return entire object as string
Print jsonNav.Stringify(), 0, "JSON Object"

End Sub

Example-2:

%REM
Sub SampleAppendObject
Description: creates this JSON object:
{
"variable1" : "value1",
"variable2" : "value2",
"myarray" : ["An","array","of","strings"],
"myobject" : {
"property1" : "yourValue1"
}
}
%END REM
Sub SampleAppendObject()
Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
Dim arr As NotesJSONArray
Dim obj As NotesJSONObject

'# create empty navigator object
Set jsonNav = session.Createjsonnavigator("")

'# append name/value pairs
Call jsonNav.appendElement("value1", "variable1")
Call jsonNav.appendElement("value2", "variable2" )
Call jsonNav.appendElement(123, "numericVariable" )

'# append array named 'myarray' with string elements
Set arr = jsonNav.appendArray("myarray")
Call arr.appendElement("An")
Call arr.appendElement("array")
Call arr.appendElement("of")
Call arr.appendElement("strings")

'# append object named 'myobject' containing a property and a value
Set obj = jsonNav.appendObject("myobject")
Call obj.appendElement("yourValue1", "property1")

'# return entire object as string
MsgBox jsonNav.Stringify(), 0, "JSON Object"
End Sub

Example-3:

%REM
Sub SampleAppendArray
Description: creates this JSON object:
{
"variable1" : "value1",
"variable2" : "value2",
"numericVariable", 123
"myarray" : ["An","array","of","strings"]
}
%END REM
Sub SampleAppendArray()
Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
Dim arr As NotesJSONArray

'# create empty navigator object
Set jsonNav = session.Createjsonnavigator("")

'# append name/value pairs
Call jsonNav.appendElement("value1", "variable1")
Call jsonNav.appendElement("value2", "variable2" )
Call jsonNav.appendElement(123, "numericVariable" )

'# append array named 'myarray' with string elements
Set arr = jsonNav.appendArray("myarray")
Call arr.appendElement("An")
Call arr.appendElement("array")
Call arr.appendElement("of")
Call arr.appendElement("strings")

'# return entire object as string
MsgBox jsonNav.Stringify(), 0, "JSON Object"
End Sub

Example-4:

%REM
Sub SampleJSONNav
Description: navigates within this JSON object
{
"variable1" : "value1",
"variable2" : "value2",
"myarray" : ["An","array","of","strings"],
"myobject" : {
"property1" : "yourValue1"
}
}
%END REM
Sub SampleJSONNav ()
Dim session As New NotesSession
Dim jsonNav As NotesJSONNavigator
Dim el As NotesJSONElement

Const testJSON$ = |
{
"variable1" : "value1",
"variable2" : "value2",
"myarray" : ["An","array","of","strings"],
"myobject" : {
"property1" : "yourValue1"
}
}
|
'# create navigator object based on the test JSON object declared before
Set jsonNav = session.Createjsonnavigator(testJSON$)

'# Search element by name
Set el = jsonNav.GetElementByName ("variable2")
MsgBox el.Value, 0, "Result of GetElementByName" '# should display 'value2'

'# Search/Get elements in JSON object:
Set el = jsonNav.GetNthElement(2)
MsgBox el.Value, 0, "Result of GetNthElement(2)" '# should display 'value2'

Set el = jsonNav.GetElementByPointer("/myobject/property1")
'# will return 'yourValue1'
MsgBox el.Value, 0, "Result of GetElementByPointer" '# should display 'yourValue1'

End Sub