Friday, June 29, 2018

101 Ways to Process JSON with PeopleCode

... well... maybe not 101 ways, but there are several!

There is a lot of justified buzz around JSON. Many of us want to (or must) generate and parse JSON with PeopleSoft. Does PeopleSoft support JSON? Yes, actually. The Documents module can generate and parse JSON. Unfortunately, many of us find the Documents module's structure too restrictive. The following is a list of several alternatives available to PeopleSoft developers:

  • Documents module
  • Undocumented JSON objects delivered by PeopleTools
  • JSON.org Java implementation (now included with PeopleTools)
  • JavaScript via Java's ScriptEngineManager

We will skip the first two options as there are many examples and references available on the internet. In this post, we will focus on the last two options in the list: JSON.org and JavaScript. Our scenario involves generating a JSON object containing a role and a list of the role's permission lists.

PeopleCode can do a lot of things, but it can't do everything. When I find a task unfit for PeopleCode, I reach out to the Java API. PeopleCode has outstanding support for Java. I regularly scan the class and classes directories of PS_HOME, looking for new libraries I can leverage from PeopleCode. One of the files in my App Server's class path is json.jar. As a person interested in JSON, how could I resist inspecting the file's contents? Upon investigation, I realized that json.jar contains the json.org Java JSON implementation. This is good news as I used to have to add this library myself. So how might we use json.jar to generate a JSON file? Here is an example

JSON.org has this really cool fluent design class named JSONStringer. If the PeopleCode editor supported custom formatting, fluent design would be really, really cool. For now, it is just cool. Here is an example of creating the same JSON using the JSONStringer:

What about reading JSON using json.org? The following example starts from the JSON string generated by JSONStringer. It is a little ugly because it requires Java Reflection to invoke the JSONObject constructor. On the positive side, though, this example demonstrates Java Class casting in PeopleCode (hat tip to tslater2006 for helping me with Java Class casting in PeopleCode)

What is that you say? Your PeopleTools installation doesn't have the json.jar (or jsimple.jar) files? If you like this approach, then I suggest working with your system administrator to deploy the JSON.org jar file to your app and/or process scheduler's Java class path

But do we really need a special library to handle JSON? By definition, JSON describes a JavaScript object. Using Java's embedded JavaScript script engine, we have full access to JavaScript. Here is a sample JavaScript file that generates the exact same JSON as the prior two examples:

... and the PeopleCode to invoke this JavaScript:

Did you see something in this post that interests you? Are you ready to take your PeopleTools skills to the next level? We offer a full line of PeopleTools training courses. Learn more at jsmpros.com.

10 comments:

Sudheer Putti said...

Hi Jim, Need some help on PeopleSoft Consuming JSON MQ message.

We configured JMSTARGET node and were able to establish the connection between MQ series and PS Local node as per PS documentation.

But we are not able to consume the JSON message. We tried to use Non-rowset message (as Peoplesoft don't have the ability to create message out of JSON Schema, we used online tool to convert JSON to XML and then got XSD and used in non-rowset creation). And created Service operation and added Handler code. But, we are not able to consume the message as it throws "Connection manager thrown General Framework Exception" error.

Then, we tried to use Document to create the message (Used same JSON Structure) and created the Service operation but when we try to consume message, it says "Unknown JMS message Format" on the error logs.

Could you please help me on to how to Consume MQ series JSON message using Document/non-rowset method?

AB Krishna said...

Hello Jim,
I get lot of solutions from your blog

I have a fluid grid page for Table of contents for out Benefits groups.
This is showing all uploaded documents based on the benefit program for the self service employee
I would like to have accordion style for sub-topics (example - Medical category has 12 sub
category documents and do not want them to be shown until sub-categoty is expanded)
I want all categories to be dynamic. I tried with group "Accordion Vertical" for the main group box and "Accordion groupbox"
on the fluid tab but I am not getting the results right since they are like master child, in sub page fashion not like accordion (just under the parent category)
I can get the things working (accordion style)with basic html/css/java script as standalone but not able to make it work in Peoplesoft environment. I am wondering whether OracleJet library accordion has any different data source for the master and child boxes?
I appreciate your input on what could be the approach
Regards - AB Krishna

Eric Christensen said...

I tried the JSONStringer class in a couple of my services and it ended up taking twice as long to return the results as my implementation using the undocumented Elastic search classes.

I have several services that return 15+ meg json objects and the serialization speed is a huge pain point. I've had to resort to caching results via a clob in the database in order to stream the data to our consuming apps quickly (minutes vs. seconds). I really wish there was a faster way so that I ditch caching.

Jim Marion said...

This is great feedback and I completely agree! The PeopleSoft apps and tools team are both recognizing the need for good JSON support. We see this with elastic, cloud chatbot service, etc. I fully expect better, documented JSON support, and I definitely do not mean the documents module.

Jim Marion said...

@AB, I think the key word there is "dynamic." There is nothing dynamic about the PeopleTools delivered Fluid Accordion. I actually wonder why they delivered it because you notice Navigation Collections and activity guides don't use it, but use something that looks and works much better. Yes, I would look else where for this functionality, especially since you want it to be dynamic.

NILAY SAXENA said...

Hi Jim,

Is it possible to create a JSON that starts with collection in Peoplesoft?

Like ["name":"TEST1","name":"TEST2"]

I tried to add the root element into the JSON array but peopelsoft is appending by default the curley bracket:

Like {[{"name":"TEST1"},{"name":"TEST2"}]}

Unknown said...

Howdy. Tried this but I am getting

Java Exception: java.lang.NoClassDefFoundError: org/json/JSONObject: finding class org.json.JSONObje ct (2,725)

Is this specific to a tools version - we are on 8.54

Thanks

Jim Marion said...

Yes, this is tools specific. I believe the Java classes were added to support Elastic Search and therefore may not exist in earlier PeopleTools releases.

Jim Marion said...

@Nilay are you using the Documents module? It is not flexible. If you start from a JSONArray, however, you will not get JSONObject notation. It looks like you may be starting from JSONObject.

Jim Marion said...

@Sudheer, I don't know that you will be able to use the Documents module or rowset based message to do this. I would be more likely to use an unstructured, non-rowset based message, which will give you a string, and parse the contents using a true JSON parser.