NotesHTTPRequest Tips

Mindwatering Incorporated

Author: Tripp W Black

Created: 03/20/2019 at 03:52 PM

 

Category:
Notes Developer Tips
LotusScript

Sample LotusScript code snippets for parsing JSON via the Domino 10.0.1 new JSON elements with the NotesHTTPRequest.
In this sample, we are getting a widget part metadata. Then we are going to get child components (parts) of the part.

The JSON data retrieved:
{
"PartNum" : "102349385",
"PartDesc" : "Modern Light Fixture A",
"Price" : "299.49",
"InStock" : true,
"MountOptions" : [
"Wallmount",
"Ceiling Flush"
"Desktop"
],
"Shades" : [
{
"SPartNum" : "A-S0113",
"SMat" : "White Fabric Shade",
"SAddCst" : "0.00",
"SInStock" : true
},
{
"SPartNum" : "A-S0110",
"SMat" : "Black Fabric Shade",
"SAddCst" : "0.00",
"SInStock" : true
},
{
"SPartNum" : "A-S0120",
"SMat" : "Copper Shade",
"SAddCst" : "49.95",
"SInStock" : true
},
{
"SPartNum" : "A-S0121",
"SMat" : "Bronze Shade",
"SAddCst" : "79.95",
"SInStock" : true
},
{
"SPartNum" : "A-S0124",
"SMat" : "Chrome Shade",
"SAddCst" : "14.49",
"SInStock" : false
}
]
}



Sub Initialize
Dim s as New NotesSession
Dim db as NotesDatabase ' current app
Dim pV as NotesView ' app config lookup view
Dim pDoc as NotesDocument ' app config profile doc
Dim doc as NotesDocument ' doc to update
Dim rGetURL as String ' GET URL location - e.g. https://www.mindwatering.net/demos/api/
Dim req as NotesHTTPRequest ' API request using getURL
Dim reqResp as Variant ' response variant containing full HTTP response
Dim rJNav as NotesJSONNavigator ' The JSON tree of the response received
Dim rjArr as NotesJSONArray ' array of results for looping
Dim partnumstr as String ' partnumber for P_Num, also used to get children parts

On Error Goto SErrorHandler

' update current document with remote JSON metadata, and update children parts
Set doc = s.DocumentContext
Set db = s.CurrentDatabase
Set pV = db.GetView("lupP")
Set pDoc = pV.GetDocumentByKey("P", True)
If (pDoc is Nothing) Then
' abort
Print "App not set-up. Aborted run."
Exit Sub
End If
rGetURL = pDoc.P_HURL(0)
If (rGetURL = "") Then
' abort
Print "App configuration missing remote server request URL. Aborted run."
Exit Sub
End If
' get response using rGetURL and populate to JSON nav
Set req= s.CreateHTTPRequest
reqResp = req.Get(rGetURL)
Set rjNav = s.CreateJSONNavigator(reqResp)

' update doc ...
' set a description string field
Call doc.ReplaceItemValue("P_Name", GetJSONVal(rjNav, "PartDesc", "1") )
' set part number field
partnumstr = GetJSONVal(rjNav, "PartNum", "1")
Call doc.ReplaceItemValue("P_Num", CLng(partnumstr ) )
' set price (double type) field
Call doc.ReplaceItemValue("P_Price", CDbl( GetJSONVal(rjNav, "Price", "1") )
' set a multivalue string field
Call doc.ReplaceItemValue("P_MntOpts", Split(GetJSONVal(rjNav, "MountOptions", "2"), ";" ) )
' set a true / false in-stock field (True = "Yes | 1 checkbox)
Call doc.ReplaceItemValue("P_InStock", GetJSONVal(rjNav, "InStock", "3") )
' save
Call doc.Save(True, False)

' update the children parts/docs
Call GetJSONChildDocs(db, rjNav, partnumstr, "Shades")


Exit Sub
SErrorHandler:
Print "(Initialize) Unexpected Error: " & Cstr(Err) & ", " & Error$ & ", line: " & Cstr(Erl) & "."
Exit Sub

Function GetJSONVal(jNav as NotesJSONNavigator, elnm as String, eldtyp as String) as String
' this function retrieves JSON node object values and returns them as string
' eldtyp 1 = string, number, and dates returned as string type, "Name", "10", "2019-02-28 10:15:01.000Z"
' eldtyp 2 = simple arrays returned as string with ";" deliminated
' eldtyp 3 = boolean, returns True as "1" and False as "0"
Dim jElem as NotesJSONElement ' JSON element node in the JSON navigator jNav
Dim jElemVal as Variant ' value returned
Dim jElArr as NotesJSONArray ' used to loop through jElem simple array values to build string with semicolon deliminators
Dim jElArrElem as NotesJSONElement ' JSON element w/in jElArr
Dim tmpstr as String ' temp working string

On Error Goto FErrorHandler

GetJSONVal = ""
If (elnm = "") Then
Exit Function
End If
Set jElem = jNav.GetElementByName(elnm)
If (jElem = Nothing) Then
Exit Function
End If
' process
Select Case eldtyp
Case "1"
jElemVal = jElem.Value
GetJSONVal = Cstr(jElemVal)
Exit Function
Case "2"
Set jElArr = jElem.Value
Set jElArrElem = jElArr.GetFirstElement
tmpstr = ""
While Not (jElArrElem is Nothing)
If Not (tmpstr = "") then
tmpstr = tmpstr & ";" ' add deliminator
End If
tmpstr = tmpstr & jElArrElem.Value
' loop
Set jElArrElem = jElArr.GetNextElement
Wend
GetJSONVal = tmpstr
Exit Function
Case "3"
jElemVal = jElem.Value
If (jElemVal = True) Then
GetJSONVal = "1"
Else
GetJSONVal = "0"
End If
Exit Function
Case Else
Exit Function
End Select

FExit:
Exit Function

FErrorHandler:
Print "(GetJSONVal) Unexpected Error: " & Cstr(Err) & ", " & Error$ & ", line: " & Cstr(Erl) & "."
Resume FExit
End Function


Function GetJSONChildDocs(db as NotesDatabase, jNav as NotesJSONNavigator, pnum as String, chelnm as String) as Integer
Dim cV as NotesView ' view of child parts for main parts
Dim chJElemMain as NotesJSONElement ' JSON element main child node in the JSON navigator jNav, contains the sub parts (e.g. shades main contains individual shades parts underneath)
' child looping part
Dim chJElemArr as NotesJSONArray ' contains the child part
Dim chjElArrElem as NotesJSONElement ' JSON element w/in jElArr - each of the child doc's JSON
Dim chjElArrElemObjVal as NotesJSONObject ' JSON w/in the chJElArrElem
Dim chjElArrElemSub as NotesJSONElement ' child attribute/field JSON element inside chjElArrElemObjVal
Dim childpartnumstr as String ' part number, used to make key
Dim childdockey as String ' child key to use for getting cDoc - parent's partnumber + "~" + child's partnumber
Dim cDoc as NotesDocument ' child doc - create if not found


On Error Goto FErrorHandler

GetJSONChildDocs = 0
Set cV = db.GetView("lupPChild")
' get main child node
Set chJElemMain = jNav.GetElementByName(chelnm)
Set chJElemArr = chJElemMain.Value
Set chjElArrElem = chJElemArr.GetFirstElement

' loop and check for docs
While Not (chElArrElem is Nothing)
Set chjElArrElemObjVal = chElArrElem.Value
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SPartNum")
childpartnumstr = chjElArrElemSub.Value
childdockey = pnum & "~" & childpartnumstr
Set cDoc = cV.GetDocumentByKey(childdockey, True)
If (cDoc is Nothing) Then
' create child part doc
Set cDoc = db.CreateDocument()
Call cDoc.ReplaceItemValue("Form", "CP")
Call cDoc.ReplaceItemValue("CP_Num", childpartnumstr )
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SMat")
Call cDoc.ReplaceItemValue("CP_Mat", chjElArrElemSub.Value )
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SAddCst")
Call cDoc.ReplaceItemValue("CP_Price", CDbl(chjElArrElemSub.Value ) )
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SInStock")
If (chjElArrElemSub.Value = True) Then
Call cDoc.ReplaceItemValue("CP_InStock", "1" )
Else
Call cDoc.ReplaceItemValue("CP_InStock", "1" )
End If
Call cDoc.Save(True, False)
Else
' child part exists, update child doc parts
' WARNING: In real life, Notes apps get big. do a wrapper to compare existing values, and only update doc if something changed. Remember the NIF.
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SMat")
Call cDoc.ReplaceItemValue("CP_Mat", chjElArrElemSub.Value )
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SAddCst")
Call cDoc.ReplaceItemValue("CP_Price", CDbl(chjElArrElemSub.Value ) )
Set chjElArrElemSub = chjElArrElemObjVal.GetElementByName("SInStock")
If (chjElArrElemSub.Value = True) Then
Call cDoc.ReplaceItemValue("CP_InStock", "1" )
Else
Call cDoc.ReplaceItemValue("CP_InStock", "1" )
End If
Call cDoc.Save(True, False)
End If


' loop
Set chElArrElem = chJElemArr.GetNextElement
Wend

FExit:
Exit Function

FErrorHandler:
Print "(GetJSONChildDocs) Unexpected Error: " & Cstr(Err) & ", " & Error$ & ", line: " & Cstr(Erl) & "."
Resume FExit
End Function

Function JSONValNullCheck(jElem as NotesJSONElement) as Integer
' returns 1 if element is nothing or type 64 / null value
' returns 0 if element is not null
If (jElem is Nothing) Then
JSONValNullCheck = 1
Exit Function
End If
If (jElem.Type = 64) Then
JSONValNullCheck = 1
Exit Function
End If
' return not null
JSONValNullCheck = 0
End Function


previous page