I Feel Pretty - Native Lotusscript Pretty Print for NotesJSONNavigator
Robert Baehr April 15 2025 03:17:23 AM
Greetings:Having been working with the Lotusscript JSON classes and dealing with the raw output of the .stringify function, I decided to write a native "pretty print" function in Lotusscript. As an example, I generated two Lotusscript NotesJSONNavigators, as shown below. Note: This is not the best "code", but merely an example.
Set jNav = s.CreateJSONNavigator("")
Set jEL = jNav.AppendElement("Vehicle Maintenance","Name")
Set jObj = jNav.AppendObject("Vehicle")
Set jEl = jObj.AppendElement("Ford","Make")
Set jEl = JObj.AppendElement("F150","Model")
Set jEl = jObj.AppendElement("2014","Year")
Set jArr = jNav.AppendArray("Oil Changes")
Set jEl = jArr.AppendElement("01-02-2025")
Set jEl = jArr.AppendElement("03-01-2025T")
Res = jnav.Stringify 'Res will contain the JSON results in raw, stringified format.
Set jNav = s.CreateJSONNavigator("")
Set jEL = jNav.AppendElement("Vehicle Maintenance","Name")
Set jObj = jNav.AppendObject("Vehicle")
Set jEl = jObj.AppendElement("Hyundai","Make")
Set jEl = JObj.AppendElement("Elantra","Model")
Set jEl = jObj.AppendElement("2010","Year")
Set jArr = jNav.AppendArray("Oil Changes")
Set jEl = jArr.AppendElement("01-12-2025")
Set jEl = jArr.AppendElement("03-23-2025T")
Res = Res & Chr(13) & Chr(10) & JNav.Stringify 'res now contains the new JSON results, appended with the carriage return/line feed, and the new JSON result in stringified format.
Printing Res will give the following, raw output.
{"Name":"Vehicle Maintenance","Vehicle":{"Make":"Ford","Model":"F150","Year":"2014"},"Oil Changes":["01-02-2025","03-01-2025T"]}
{"Name":"Vehicle Maintenance","Vehicle":{"Make":"Hyundai","Model":"Elantra","Year":"2010"},"Oil Changes":["01-12-2025","03-23-2025T"]}
While accurate, this format can leave a lot to desire visually (especially with large data sets). I devised a function called BWSJSONPrettyPrint that will "pretty print" (format) the resulting NotesJSONNavigator.stringify. BWSJSONPrettyPrint takes two parameters:
Parameter 1: The stringified version of the JSON results, and
Parameter 2: The number of spaces that you want to indent the results by
For the example above, if I call BWSJSONPrettyPrint(Res,5), we get the following output (indented by 5 spaces)
{
"Name": "Vehicle Maintenance",
"Vehicle": {
"Make": "Ford",
"Model": "F150",
"Year": "2014"
},
"Oil Changes": [
"01-02-2025",
"03-01-2025T"
]
}
{
"Name": "Vehicle Maintenance",
"Vehicle": {
"Make": "Hyundai",
"Model": "Elantra",
"Year": "2010"
},
"Oil Changes": [
"01-12-2025",
"03-23-2025T"
]
}
This produces a much easier to read JSON data structure, without the need to copy/paste into a formatter. As as added bonus, if you save the output to a file, you get the same "pretty" result.
Thought I'd share why I still believe that you can do darned near anything with Domino.....
Cheers!
Bob Baehr
The Unofficial Poster Child for Notes and Domino
- Comments [0]