Create / Write JSON File PeopleCode Example

Let us see how to write data from PeopleSoft application as a JSON File in this tutorial. This piece of code can fit into Application Engine program or even in your record peoplecode and is so simple.We will use an open source Java library JSON.simple for this purpose.You don't need to be a Java expert, as you will appreciate the easiness with which you can generate JSON data at the end of this post.

1.Write JSON From PeopleCode - Basic Example


We begin with creating a Java Object of type org.json.simple.JSONObject in PeopleCode. You can then use the put method to add JSON data. Once this is done, you can use java.io.FileWriter and write the contents in your JSONObject to file. This is the very basic approach to create JSON file and the full PeopleCode for this is shown below:

/* PeopleCode Program - Create and Write JSON File */

/* Create a JSON Object to Add Data */
Local JavaObject &myJson = CreateJavaObject("org.json.simple.JSONObject");

/* Now add some content */
&myJson.put("Name", "test");
&myJson.put("Place", "California");
&myJson.put("Planet", "Earth");

/* Create JSON Output File */
Local JavaObject &ioWriter = CreateJavaObject("java.io.FileWriter", "myJson.json");


/* Write JSON Data to File */
&ioWriter.write(&myJson.toJSONString());

/* Close File */
&ioWriter.flush();
&ioWriter.close();


The output of this program is provided below:
{"Planet":"Earth","Place":"California","Name":"test"}

This library also offers lot of additional methods using which you can write different JSON data from PeopleCode. These are shown below.

2.Write Number Data to JSON File from PeopleCode


You can easily add a line of code to the example above that can add Number data to JSON File from PeopleCode. This is shown below:
/* Add Integer Data to JSON File */
&myJson.put("Counter", CreateJavaObject("java.lang.Integer", 100));

The output JSON File data is provided below:
{"Planet":"Earth","Place":"California","Counter":100,"Name":"test"}

3. Add JSON Boolean Data from PeopleCode


On the same lines, you can easily add boolean data from PeopleCode to JSON File. A sample PeopleCode that does this is provided below:
/* Insert Boolean Data to JSON File */
&myJson.put("isChecked", CreateJavaObject("java.lang.Boolean", "true"));

The output of this code inserts boolean data to JSON File as per below:
{"Planet":"Earth","Place":"California","Counter":100,"Name":"test","isChecked":true}

4. Write JSON Array From PeopleCode


Finally, you can also add an array of JSON data using org.json.simple.JSONArray class. For the example we provided above, we can extend and add JSON array as per below:
/* Create JSON Array Object*/
Local JavaObject &myJsonArray = CreateJavaObject("org.json.simple.JSONArray");
&myJsonArray.add("Item1");
&myJsonArray.add("Item2");
/* Add Array back to Original JSON Object */
&myJson.put("ArrayData", &myJsonArray);

The output is shown below:
{"Planet":"Earth","ArrayData":["Item1","Item2"],"Place":"California","Counter":100,"Name":"test","isChecked":true}

5. Write Array of JSON Objects in PeopleCode


Finally, a JSON array can contain JSON objects and you can keep repeating the example above to add as many JSON data as required. This is shown below with an example output;
/* Add Array of JSON Objects*/
Local JavaObject &arrayObect = CreateJavaObject("org.json.simple.JSONObject");
&arrayObect.put("S1", "1");
&arrayObect.put("S2", "1");
Local JavaObject &jSonArray = CreateJavaObject("org.json.simple.JSONArray");
&jSonArray.add(&arrayObect);
/* Add object array back to original JSON */
&myJson.put("ObjectArray", &jSonArray);

/* produces an output of */
{"Planet":"Earth","ArrayData":["Item1","Item2"],"Place":"California","Counter":100,"Name":"test","ObjectArray":[{"S2":"1","S1":"1"}],"isChecked":true}

Hope this tutorial simplifies your requirement to create JSON data from PeopleSoft applications. If you have a question, you can post it in the comments section below. See you in a different tutorial next time. You can download the code used in this example here.

Download the JSON.simple JAR File and upload it to your class folder in PeopleSoft without fail. 

1 comment:

  1. Hi, 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.



    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 GeneralFrameworkException" error.



    Then, we tried to use Document to create the message (Created same JSON structure document) and created the Service operation but when we try to consume message, it says "Unknown JMS message Format".



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

    ReplyDelete