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.

20 comments:

Unknown 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

ericdrum 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.

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.

Unknown said...

Do you know if this is available in 8.56?

Jim Marion said...

Yes, absolutely! Every single one of these methods is available in 8.56.

muthu said...

Hi Jim,

We have created a REST web services and response in JSON message. Whether document builder in PeopleSoft can create a dynamic JSON structure? for example document has three collection A, B,C. I have to send only the data in A, C or B,C based on the request. We are in 8.57

Jim Marion said...

I don't know that you can make the Document dynamic, but you can create multiple documents and choose which one to generate. If the Service Operation is attached to an unstructured message, then it works perfectly. This would not work with a Service Operation attached to a Document-based message. Easy fix, though, just change the Service Operation (or create a new version).

Jie said...

Hi, Jim
Thank you so much for sharing. We are on PeopleTools 8.51, do you know what is the good way to process JSON? Thank you!

Jie

Jim Marion said...

@Jie, on 8.51, it is not easy but is possible. I think a Java library would be easiest. I have had great success with the org.json JSON classes and JSON.simple. We documented JSON.simple in our PeopleTools Tips and Techniques book written on PeopleTools 8.49.

Viktor said...

Hello Jim,

Thank you for your work. I wondered if you have any example(s) how to send back a confirmation message from a message OP (POST) json. Parsing a data in json and using services OP and message POST from a third-party is working but sending back to the third-party a confirmation "success" can't find a good way to do this. The code is in a class and method.

Thanks,

Anonymous said...

Hi Jim,

Is there a way to read dynamic Json documents and get the respestive field values like emplid,effdt. i am having multiple Json documents each with different structure and details like one with emplid,effdt,hiredt etc, other document with orderid,refid,etc..,how to load this document and identify the field names and values in peoplecode. PLease help me in understanding a way to do this.

Jim Marion said...

@Anonymous, use JsonParser to parse JSON content, and then you may inspect the contents.

Jim Marion said...

@Viktor, take a look at http://blog.jsmpros.com/2011/10/rest-like-peoplesoft-services.html and the psnonxml section.

Viktor said...

Hi Jim, I've completed the project. The process was interesting; in fact, I was able to incorporate CI and code's classes and methods. This type of integration was fascinating and has opened up many new opportunities.

Thank you for answering, your work "blog" is very good.

Best,

Victor P. Unda :)