Thursday, January 26, 2017

PeopleCode & JSON

For quite some time, I have been trying to use PS Documents Technology to play around with JSON, but given its limitation, haven't been able to deal with dynamic content. Along comes ElasticSearch in PeopleSoft, and that shows the way to not only just create JSON documents but also parse them.
Credits to Chris Malek (his article is a must read) for showing us the way. Most of the API is still undocumented, but you can make use of PeopleCode Auto-completion feature to explore the API methods and properties. It seems a lot has been borrowed from Groovy. A good place to start is the delivered ES App Pkg - PTSF_ES:*. I also went ahead and created a sample python script for the same REST end point. Python makes your life a lot easier. Let's have a look at a sample in PeopleCode first.

I will break it down into steps for easier understanding:

  1. Make a GET request on a given REST end point. It is easier to bypass the Integration Engine and make use of IB ConnectorRequest to initiate the sync request.
  2. Instantiate a JSONParser and read the response JSON;
  3. Read the JSONArray and retrieve the Property and print the values;
REST end point we will use for creating this sample -
As long as we know the name of the key(s) which are part of he response JSON, we can write PeopleCode to deal with it. POSTMAN is a great REST client to explore REST services.

Make a GET Request in PeopleCode
Use delivered IB_GENERIC message definition and dynamically load the connector and its properties. In this case, I am making a GET request. It is important to set the Content-Type. In case your REST end point mandates use of Authorization (Basic, Digest, etc.,), set that as property. While making the request, note that I have set the boolean to TRUE, which means, the code will have to deal with IB Exception Object.
IB ConnectorRequest
IB Connector Request

Instantiate a JSONParser
Use the parser object and retrieve the Root Object. Check for key "RestResponse", and read the return array.
Read the JSONArray
The response returns the list of all the countries and for this example I have use index 248 to read the values. In this case, index 248 points to country Zambia.
Read JSONArray
This example is just to give you an idea on how to parse JSON documents in PeopleCode. I am sure, there is way more to this, and I will update this as I explore further.
Credits to Cameron Barre for sharing his JSON encoding library


No comments: