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.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. A question I get asked a lot about my preference of using GSON, why use JSON.simple as opposed to the included com.ibm.commons.util.io.json package? I'm curious as to how they compare, feature set wise, as they seem to generally read about the same (JSONObject vs JsonJavaObject, building an object manually, etc.).

    ReplyDelete
    Replies
    1. Check the link I posted in my conclusion, it gives a nice comparison test between 4 options. iBM commons is not one of them though.

      Delete
  3. @Eric I am not aware of solid reasons to use JSON.simple over GSON or Jackson or Boon or Genson -- all of which are (in my opinion) much better libraries for JSON handling. Neither JSON.simple nor org.json library supports anything other than simple tree model; they are not particularly fast, easy to use. If you are happy with GSON I see no reason to consider JSON.simple as an alternative.

    ReplyDelete
    Replies
    1. It's more of a curiosity, as I'm less familiar with its feature set (added some app maintenance to an app that used it, but that's it). I'm definitely happy with being able to perform object reflection with those solutions that can do so.

      Delete
    2. Eric, I also think that there is no solid reason to use JSON.simple if you already know GSON or another toolkit. When I developed this I forgot about the IBM commons. I am presenting this as an option, but not claiming it is a better option.

      Delete