Read / Parse JSON File PeopleCode Example

Let us examine how to read / parse JSON Data in PeopleCode with an example in this post. We will be using JSON.simple JAR files for this tutorial. JSON.simple is an open source library, compatible with your PeopleSoft JRE. The input JSON data will be available in a file named input.json.


1. Read / Parse JSON PeopleCode Example


We begin with a simple setup to start with.The test JSON file that will be used (across this tutorial )is provided below:

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

We will read 'Planet' and 'Place' information from this JSON data in PeopleCode. The example is shown below:

/* PeopleCode Program - Read / Parse JSON */

/*Use JSONParser and Parse input JSON File in PeopleCode */
Local JavaObject &parseJson = CreateJavaObject("org.json.simple.parser.JSONParser");
Local JavaObject &parseOutput = &parseJson.parseReader(CreateJavaObject("java.io.FileReader", "input.json"));

/* Read and Retrieve JSON Data */
/* Print JSON output through MessageBox */
MessageBox(0, "", 0, 0, "Place" | &parseOutput.get("Place").toString());
MessageBox(0, "", 0, 0, "Planet" | &parseOutput.get("Planet").toString());

Note that we are using a method parseReader, which returns an object of type org.json.simple.JSONObject. I have customized JSON.simple instead of using reflection techniques to get around "more than one overload matches" error. The Java Code I added to org.json.simple.parser.JSONParser is provided below. A modified version of JAR file is available for download at the end of this post.

/* New Method to Prevent Ugly Reflection Techniques in PeopleCode */
public JSONObject parseReader(Reader in) throws IOException, ParseException{
        return (JSONObject) parse(in, (ContainerFactory)null);
}

2.Parse Boolean JSON Data Type - PeopleCode Example


If you want to read Boolean data and assign it to a boolean variable, you have to do simple modifications to the PeopleCode provided earlier. This is shown below: [ You can read other data types like integer on the same approach ]
/* Read Boolean Data from JSON file to PeopleCode variable */
Local boolean &blJson = &parseOutput.get("isChecked").toString();
MessageBox(0, "", 0, 0, "Boolean Data" | &blJson);

3. Reading JSON Array in PeopleCode


We will discuss how to parse JSON Array using PeopleCode. In the input JSON File, we have a JSON Array with two elements and we will print them from PeopleCode. Again, to avoid reflection issues, we will add a new method in& org.json.simple.JSONObject, that will take the ID Element and return an object of type org.json.simple.JSONArray back. This is shown below:

/* Customization for PeopleSoft - to Return JSONArray from String */
public JSONArray getJSONArray(String aData){
        return (JSONArray) this.get(aData);
}

You can now retrieve a JSON Array, parse it and print the contents and size of the array from PeopleCode with ease. A PeopleCode snippet that does this is shown below:

/* Read JSON Array */
Local JavaObject &jsonArray = &parseOutput.getJSONArray("ArrayData");
/* Print JSON Array as String */
MessageBox(0, "", 0, 0, "JSON Array as String" | &jsonArray.toString());
/* Get Number of Elements in Array  */
MessageBox(0, "", 0, 0, "JSON Array Length" | &jsonArray.size());
/* Print individual elements in Array */
MessageBox(0, "", 0, 0, "JSON Element 1 " | &jsonArray.get(0).toString());
MessageBox(0, "", 0, 0, "JSON Element 2 " | &jsonArray.get(1).toString());

If your JSON array has mixed datatypes (boolean, integer) you can read them using same approach.


4. Parse Array of JSON Objects


Extending above example, you can also parse and read an array of JSON objects with PeopleCode. Each element in the JSONArray is an object of type JSONObject. Using the same input file, you can print the "ObjectArray" as shown below:

/* Parse Array of JSON Objects */
Local JavaObject &arrayOfJson = &parseOutput.getJSONArray("ObjectArray");
/* Get Number of JSON Objects in the Array */
MessageBox(0, "", 0, 0, "Total JSON Objects " | &arrayOfJson.size());
/* Get JSON Object of First Element */
/* Put this into a FOR LOOP to read every JSON Object line by line in the Array */
Local JavaObject &arrayObject = &arrayOfJson.getJSONObject(&arrayOfJson.get(0));
MessageBox(0, "", 0, 0, "Value of S2 " | &arrayObject.get("S2").toString());
MessageBox(0, "", 0, 0, "Value of S1 " | &arrayObject.get("S1").toString());

To get this working, add a new method in org.json.simple.JSONArray as shown below (to cast an object):
/* Get Object and Return JSONObject*/
public JSONObject getJSONObject(Object o){
                return (JSONObject)o;
}

The whole tutorial requires a bit of customization to JSON.simple, but it is worth as reading JSON from PeopleCode is greatly simplified through this approach. Finally, here are some of the artefacts that you can download for this tutorial: