Looking through the PeopleTools 8.57 Feature Overview document, you may have noticed that 8.57 now includes support for several JSON classes as well as PeopleBooks documentation. As Chris Malek showed us a couple of years ago, the classes listed in the Feature Overview document are not new. What is new is the keyword Support and PeopleBooks documentation. Using the documentation, I was able to generate a sample on PeopleTools 8.57:
Local JsonBuilder &jbldr = CreateJsonBuilder(); Local string &json; If &jbldr.StartArrayReturnsTrue("Employees") Then REM Empl 1; If &jbldr.StartObjectReturnsTrue("Employee") Then If &jbldr.StartObjectReturnsTrue("Name") Then &jbldr.AddProperty("First", "Jim"); &jbldr.AddProperty("Last", "Marion"); &jbldr.AddProperty("Middle", "J"); &jbldr.EndObject("Name"); End-If; &jbldr.AddProperty("ID", 123456); &jbldr.EndObject("Employee"); End-If; REM Empl 2; If &jbldr.StartObjectReturnsTrue("Employee") Then If &jbldr.StartObjectReturnsTrue("Name") Then &jbldr.AddProperty("First", "Lucy"); &jbldr.AddProperty("Last", "McGillicuddy"); &jbldr.AddProperty("Middle", ""); &jbldr.EndObject("Name"); End-If; &jbldr.AddProperty("ID", 789123); &jbldr.EndObject("Employee"); End-If; &jbldr.EndArray("Employees"); End-If; &json = &jbldr.ToString(); MessageBox(0, "", 0, 0, &json);
Alternatively, we can build JSON structures using JsonObject and JsonArray directly, but I like the way the JsonBuilder structures code so that child items appear indented, etc. Notice the code above begins the JSON structure with an array? Here is the output. Notice the root node is an object, not an Array:
Even though my very first call to JsonBuilder was to start an Array, it started an Object. What if you just want an array as the outer node? We can extract the array from the JsonBuilder RootNode using the following:
&jbldr.GetRootNode().GetJsonObject().GetJsonArray("Employees");What if we want to format the code? First, I don't recommend formatting code you will transmit to external systems as white-space compressed JSON is preferred for data transmission. But formatting for debugging purposes is perfect. We can format JsonBuilder output using the JsonGenerator class. Here is a fragment that will format the JsonBuilder result:
Local JsonGenerator &jgen = CreateJsonGenerator(); &jgen.SetRootNode(&jbldr.GetRootNode()); &json = &jgen.ToString();
One thing to note is that JsonBuilder will let you generate invalid JSON. The parameter to StartXxxReturnsTrue is the name of the node to create. If we start the first node with a zero-length string:
&jbldr.StartArrayReturnsTrue(""), then the generated JSON will include curly brace object notation, but no property name before the Array start.
As I look through the documentation for 8.57, I see every Json class method and property documented, but what about the CreateJsonXxx functions? Anyone find documentation for these functions? Did I miss something?
As Chris pointed out, these JSON Classes have been in PeopleTools since 8.55.11. Assuming that just the documentation is new and not the classes, I ran all of this code on 8.56 and it works without modification.