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 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("", "myJson.json");

/* Write JSON Data to File */

/* Close File */

The output of this program is provided below:

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:

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:

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");
/* Add Array back to Original JSON Object */
&myJson.put("ArrayData", &myJsonArray);

The output is shown below:

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");
/* Add object array back to original JSON */
&myJson.put("ObjectArray", &jSonArray);

/* produces an output of */

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.