Thursday, October 1, 2015

Creating JSON objects in Java using the JSON.simple toolkit

I needed a way to create JSON objects in Java for displaying in a grid. There are several options for doing this that I could have chosen. There is GSON from Google, or I could roll my own using StringBuilder. After doing some research, I decided to use the JSON.simple toolkit because, the examples looked simple and straightforward to me.  In this post, I will explain how I used JSON.simple and give two real life examples.


Installation

To use JSON.simple you need to download the jar and add to your build path. The jar is available here: https://code.google.com/p/json-simple/


Creating a Single Object


In this snippet, contents of a NotesDocument are added to a JSON object and then that object is passed back to the caller of the method. Note this example uses the Name class from the original Notes,jar to format the Notes name. It is the java version of @Name.

Document doc = col.getFirstDocument();
JSONObject json = new JSONObject();

if(doc != null){
   Name name = session.createName(doc.getItemValueString("EmployeeName"));
   json.put("employeeName", name.getCommon());
   json.put("employeeID", doc.getItemValueString("EmployeeID"));
   json.put("employeeLocation", doc.getItemValueString("locationdesc"));
   json.put("employeeRole", getEmployeeRole(name.getCommon()));
}
returnValue = json.toJSONString();
...
return returnValue;

The result will look something like this:

{"employeeLocation":"Pensacola Milton","employeeRole":"Intern to the Intern","employeeID":"12345","employeeName":"Dwain Wuerfel"}


Creating a Collection of Objects


In this snippet, the contents of a multi-value document are traversed and a JSON object is created for each item. Many will recognize that I am grabbing the members of a Name & Address Book group. For each member I create a new object and then that object is added to a JSONArray object called ‘result’ which is then returned to the calling method. Each object will be a displayed on a separate row in the grid.

JSONArray result = new JSONArray();

if(nabDoc != null){
   Item item = nabDoc.getFirstItem("Members");
   Vector<String> v = item.getValues();

   for(String person : v){
   Name name = session.createName(person);

   JSONObject json = new JSONObject();
   json.put("employeeName", name.getCommon());
   json.put("employeeRole", employeeRole);
   json.put("employeeID", EmployeeID);
   json.put("employeeLocation", EmployeeLocation);
   result.add(json);
}
returnValue = result.toJSONString();
...
return returnValue;

The result will look like this if there are three JSON objects in the array:

 [{"employeeLocation":"Pensacola Milton","employeeRole":["Minion"],"employeeID":"12345","employeeName":"Dwain Wuerfel"}, {"employeeLocation":"Pensacola Milton","employeeRole":["Blog Author"],"employeeID":"91984","employeeName":"Steve Zavocki"},{"employeeLocation":"Pensacola Milton","employeeRole":["Cast Member"],"employeeID":"54321","employeeName":"Vernon Miles"}] 

Conclusion


As the name implies the JSON.simple toolkit is simple to use, and a good option to consider if you need to create JSON objects within your java code. In case you are interested in learning more about your other options, here is an article that compares three different json toolkits including JSON.simple.