Technical Blog Post
Abstract
Creating a JSON String from JSON Object and JSON Arrays in Automation Scripts
Body
Maximo framework includes json4j.jar library for JSON implementations such as constructing JSON formatted data, XML to JSON conversions, JSON string and stream parsing etc.
More information on JSON and JSON4J library can be found via the link below:
My starting point for this exercise was the requirement of a REST client implemented on Maximo for an external REST service. I needed to send a JSON formatted text as the body of an http POST request. For clarification, The JSON body holds data from Maximo objects which will be sent to the external system to create associated records there.
For single objects we use JSONObject alone which is an easy task. You can see the example code piece below.
# creating a JSON String (directly executed via Run Automation Script button) from com.ibm.json.java import JSONObject from sys import * # method for creating a JSON formatted String def createJSONstring(): obj = JSONObject() obj.put('FIELD_1', 'VALUE_1') obj.put('FIELD_2', 0) obj.put('FIELD_3', 1.1) obj.put('FIELD_4', True) # add as many fields as needed ... jsonStr = obj.serialize(True) return jsonStr # main part str = createJSONstring() print str
Code 1 - Creating a JSON Formatted String
The Output of Code 1 in Script Results Window of Automation Scripts application is as follows:
{
"FIELD_1": "VALUE_1",
"FIELD_2": 0,
"FIELD_3": 1.1,
"FIELD_4": true
}
The second exercise is more interesting since we will use both JSONObject and JSONArray in order to send a parent record together with its two child records. Below, you can see the code piece for this task:
# creating a JSON String with an array (directly executed via Run Automation Script button) from com.ibm.json.java import JSONObject, JSONArray from sys import * # method for creating a JSON formatted String including an array within def createJSONstring(): # defining the first child object ch1_obj = JSONObject() ch1_obj.put('CH_FIELD_1', 1) ch1_obj.put('CH_FIELD_2', 'VALUE_2') # defining the second child object ch2_obj = JSONObject() ch2_obj.put('CH_FIELD_1', 2) ch2_obj.put('CH_FIELD_2', 'VALUE_3') # adding child objects to children array ch_arr = JSONArray() ch_arr.add(ch1_obj) ch_arr.add(ch2_obj) # putting the array into children object ch_obj = JSONObject() ch_obj.put("children", ch_arr) obj = JSONObject() obj.put('FIELD_1', 0) obj.put('FIELD_2', 'VALUE_1') obj.put('FIELD_3', 1.1) obj.put('FIELD_4', True) # putting children object into related records key obj.put("RELATED_RECORDS", ch_obj) # add as many fields as needed ... jsonStr = obj.serialize(True) return jsonStr # main part str = createJSONstring() print str
Code 2 - Creating a JSON Formatted String including JSON Array
The Output of Code 2 in Script Results Window of Automation Scripts application is as follows:
{
"FIELD_1": 0,
"FIELD_2": "VALUE_1",
"FIELD_3": 1.1,
"FIELD_4": true,
"RELATED_RECORDS": {
"children": [
{
"CH_FIELD_1": 1,
"CH_FIELD_2": "VALUE_2"
},
{
"CH_FIELD_1": 2,
"CH_FIELD_2": "VALUE_3"
}
]
}
}
UID
ibm11129767