JSON Parsing using PeopleCode Classes Starting in PeopleTools 8.55.11
By Chris Malek | Tue, Jan 10, 2017
Update July 2018: I wrote a more detailed article called JSON Parsing Using PeopleTools JsonParser
JSONtoXML Application Class - An Alternative Method to Parse JSON in Peoplecode
I was listening to the the psadmin.io podcast and Kyle and Dan were talking about the newly released Elasticsearch support in PeopleTools 8.55.11. Dan Iverson mentioned that he found some PeopleCode that was interfacing with ElasticSearch that was generating and parsing JSON. This immediately peaked my interest. I have been struggling to work with JSON in PeopleSoft for a few years now and I have only been disappointed. I had written two previous articles about my work with JSON in PeopleCode:
- JSON Parsing Limitations in 8.53
- JSONtoXML Application Class - An Alternative Method to Parse JSON in Peoplecode
I also submitted an Oracle Support “idea” that as of the time of writing had 70 up votes: JSON Support similar to XML PeopleCode Classes
I have struggled with the document technology myself and have completely abandoned its use. It would make sense that the “static structure” you must generate with the Document Technology would not work with Elasticsearch. From my limited work and reading on Elasticsearch, the only way to interact with the system is over a REST service that receives and sends JSON only. There is no XML, no SOAP, no real GUI to administer it by default.
So PeopleTools needs some way to dynamically generate and parse JSON when interfacing with the Elasticsearch. Thanks to Dan’s comment I installed a DPK based on PeopleTools 8.55.11. Kyle and Dan were helpful again in this regard because they published a free Deployment Package Quick Start Course which gave me everything I needed to get a 8.55.11 instance running on AWS. This probably saved me hours of reading DPK documents. Since I am really a developer and not an PS Admin, I just needed the basics of getting a DPK running.
I went and found the Elasticsearch PeopleCode which lives in Application Package: PTSF_ES:*
. Where I found references to two new PeopleCode classes: JsonBuilder
and JsonObject
. It looks like JsonBuilder
is used for creating a JSON object. Here is a code snippet pulled out of some delivered code relating to Elasticsearch.
Local JsonBuilder &jbldr = CreateJsonBuilder();
If &jbldr.StartObjectReturnsTrue("") Then
/* Specifications for Index creation */
If &jbldr.StartObjectReturnsTrue("settings") Then
If &jbldr.StartObjectReturnsTrue("index") Then
&jbldr.AddProperty("number_of_shards", &nShards);
&jbldr.AddProperty("number_of_replicas", &nReplicas);
&jbldr.EndObject("index");
End-If;
%This.AddIndexAnalysers(&jbldr, &saActiveLangs);
&jbldr.EndObject("settings");
End-If;
&jbldr.EndObject("");
End-If;
This would build JSON like this:
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 2
}
}
}
The JsonObject
is used for parsing a JSON String. Here is an example of Elasticsearch Ping
method. In the code below you will see that:
- a
JsonObject
is declared and instatiated into&response
. - This code runs
%This.doService(&m_url, "GET", Null);
which will end up calling some Elasticsearch REST HTTP Service which will return JSON into&response
. - Then the
JsonObject.IsExist
method is called to look for JSON object calledversion
. If that exists then it pulls out the actual version number from thenumber
JSON node. - There are further checks looking for
error
nodes in the JSON.
method Ping
/+ Returns String +/
/+ Extends/implements PT_SEARCH:SearchAdminService.Ping +/
/* Ping returns the response to the getApiVersion op */
Local string &ret = "";
Local JsonObject &response = %This.doService(&m_url, "GET", Null);
If &response.IsExist("version") Then
Local JsonObject &version = &response.GetJsonObject("version");
Local string &versionStr = &version.GetString("number");
&ret = "Elasticsearch Version " | &versionStr;
End-If;
If &response.IsExist("error") Then
If &response.GetNumber("status") = 401 Then
Local string &errorStr = &response.GetString("error");
throw CreateException(0, 0, &errorStr);
End-If;
End-If;
If &ret = "" Then
throw CreateException(0, 0, "Failed");
End-If;
Return &ret;
end-method;
This is a very welcome change to PeopleTools. Having dynamic JSON parsing and creation built into PeopleTools is way past due. At the time of writing I found zero hits on Oracle support of PeopleBooks for JsonObject
or JsonBuilder
. I am sure that there will be some documentation at some point. Luckily you can use the code completion to peak at the methods and properties.
Here is what the JsonObject
looks like.
Article Categories
Chris Malek
Chris Malek is a PeopleTools® Technical Consultant with two decades of experience working on PeopleSoft enterprise software projects. He is available for consulting engagements.
About Chris Work with ChrisPeopleSoft Simple Web Services (SWS)
Introducing a small but powerful PeopleSoft bolt-on that makes web services very easy. If you have a SQL statement, you can turn that into a web service in PeopleSoft in a few minutes.
Integration Broker - The Missing Manual
I am in the process of writing a book called "Integration Broker - The Missing Manual" that you can read online.