RESTful JSON Service

This example builds on the RESTful Web Service example by adding a service class that produces a Tablespoons to Cups conversion chart with JSON.

The JSON service loads and parses a text file that contains a list of JSON-formatted tablespoons to cups conversions. JSON is an ordered collection, so loading and parsing a file was the best way to have the list entries maintain their order when displayed by the service.

JSON formatting is not easy for the human eye to parse. You would want to convert the JSON to XML, put the XML through an XLST transformer, and then create a formatted web page. This example includes the code to convert the JSON object to XML so you can see what that looks like (its reproduced at the end of this post).

The so-called XML output requires intricate processing to make it into proper XML to make it ready to be read by an XLST transformer. The intricate processing steps are not covered in this example, but would make a good exercise.


Before you begin, follow the instructions to create the RESTful Web Service example, but name this project UIJSONBakerRESTJerseyServer. Make sure you change the name in the pom.xml and web.xml files for the new UIJSONBakerRESTJerseyServer project.

  1. In the package, add the DisplayChartService class.
  2. Copy the following code into the DisplayChartService class file.
  3. Copy the tbstocups.txt file (that follows the DisplayChartService code) to your computer.

DisplayChartService Class:

If you get errors next to the org.json.simple packages, add the  com.googlecode.json-simple dependency to your pom file.

  1. Open the pom.xml file in the Maven POM Editor
  2. Select the Dependencies tab and click Add.
  3. Search on googlecode.json-simple and select version 1.1
  4. Save the POM file and clean your project (Project > Clean).

import java.util.Iterator;
import org.json.XML;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class DisplayChartService { 
  public Response displayChart() throws Exception {      
    JSONArray my_data = new JSONArray();        
    my_data = parseJSON();
    String xml = XML.toString(my_data, "TablespoonsToCups");
    String result = "@Produces(\"application/json\") Output: \n\nTbs to Cups \n\n" + my_data;
    return Response.status(200).entity(result).build();
  private JSONArray parseJSON() {
    JSONParser parser = new JSONParser();
    Object obj;
    JSONArray conversionList=null;
    try {
     //Provide the location of your tbstocups.txt file for upload.
      obj = parser.parse(new FileReader(
      JSONObject jsonObject = (JSONObject) obj;
      String tablespoons = (String) jsonObject.get("tablespoons");
      String cups = (String) jsonObject.get("cups"); 
      conversionList = (JSONArray) jsonObject.get("Tablespoons to Cups"); 
      System.out.println("Tablespoons: " + tablespoons); 
      System.out.println("Cups: " + cups);
      System.out.println("\nTablespoons to Cups:\n");
      Iterator<String> iterator = conversionList.iterator();
      while (iterator.hasNext()) {
    } catch (Exception e) {
    return conversionList;    


tbstocups.txt File:

{"Tablespoons to Cups" [
        "tablespoons: 16",
        "cups: 1.000000000 (1)"
        "tablespoons: 15",
        "cups: 0.093750000 (9/16)"
        "tablespoons: 14",
        "cups: 0.875000000 (7/8)"
        "tablespoons: 13",
        "cups: 0.812500000 (13/16)"
        "tablespoons: 12",
        "cups: 0.750000000 (3/4)"
        "tablespoons: 11",
        "cups: 0.687500000 (11/16)"
        "tablespoons: 10.66666",
        "cups: 0.666666250 (2/3)"
        "tablespoons: 10",
        "cups: 0.625000000 (5/8)"
        "tablespoons: 09",
        "cups: 0.562500000 (9/16)"
        "tablespoons: 08",
        "cups: 0.500000000 (1/2)"
        "tablespoons: 07",
        "cups: 1.043750000 (7/16)"
        "tablespoons: 06",
        "cups: 0.375000000 (3/8)"
        "tablespoons: 05.33333",
        "cups: 0.333333125 (1/3)"
        "tablespoons: 05",
        "cups: 0.312500000 (5/16)"
        "tablespoons: 04",
        "cups: 0.250000000 (1/4)"
        "tablespoons: 03",
        "cups: 0.187500000 (3/16)"
        "tablespoons: 02",
        "cups: 0.125000000 (1/8)"
        "tablespoons: 01",
        "cups: 0.062500000 (1/16)"

Then  . . .

Specific instructions for the following steps are in the RESTful Web Service example:

  1. Clean, Update, and Build the service .
  2. Deploy the project to the server.
  3. Start Tomcat server.

Test Your Web Service

The URL for this service is:


The output looks like this:

@Produces("application/json") Output: 

Tbs to Cups 

["tablespoons: 16","cups: 1.000000000 (1)","tablespoons: 15","cups: 0.093750000 (9\/16)","
tablespoons: 14","cups: 0.875000000 (7\/8)","tablespoons: 13","cups: 0.812500000 (13\/16)","
tablespoons: 12","cups: 0.750000000 (3\/4)","tablespoons: 11","cups: 0.687500000 (11\/16)","
tablespoons: 10.66666","cups: 0.666666250 (2\/3)","tablespoons: 10","cups: 0.625000000 (5\/8)","
tablespoons: 09","cups: 0.562500000 (9\/16)","tablespoons: 08","cups: 0.500000000 (1\/2)","
tablespoons: 07","cups: 1.043750000 (7\/16)","tablespoons: 06","cups: 0.375000000 (3\/8)","
tablespoons: 05.33333","cups: 0.333333125 (1\/3)","tablespoons: 05","cups: 0.312500000 (5\/16)","
tablespoons: 04","cups: 0.250000000 (1\/4)","tablespoons: 03","cups: 0.187500000 (3\/16)","
tablespoons: 02","cups: 0.125000000 (1\/8)","tablespoons: 01","cups: 0.062500000 (1\/16)"]

Console Window Output

This example displays output in the console window. You can see the tablespoon and cup values:

tablespoons: 16
cups: 1.000000000 (1)
tablespoons: 15
cups: 0.093750000 (9/16)
tablespoons: 14
cups: 0.875000000 (7/8)
tablespoons: 13
cups: 0.812500000 (13/16)

At the very end is an XML string created by the following lines in the code:

Convert JSONObject to XML:

JSONArray my_data = new JSONArray();        
my_data = parseJSON();
String xml = XML.toString(my_data, "TablespoonsToCups");

XML output:

<TablespoonsToCups>[&quot;tablespoons: 16&quot;,&quot;cups: 1.000000000 (1)&quot;,&quot;tablespoons: 15&quot;,&quot;cups: 0.093750000 (9\/16)&quot;,&quot;tablespoons: 14&quot;,&quot;cups: 0.875000000 (7\/8)&quot;,&quot;tablespoons: 13&quot;,&quot;cups: 0.812500000 (13\/16)&quot;,&quot;tablespoons: 12&quot;,&quot;cups: 0.750000000 (3\/4)&quot;,&quot;tablespoons: 11&quot;,&quot;cups: 0.687500000 (11\/16)&quot;,&quot;tablespoons: 10.66666&quot;,&quot;cups: 0.666666250 (2\/3)&quot;,&quot;tablespoons: 10&quot;,&quot;cups: 0.625000000 (5\/8)&quot;,&quot;tablespoons: 09&quot;,&quot;cups: 0.562500000 (9\/16)&quot;,&quot;tablespoons: 08&quot;,&quot;cups: 0.500000000 (1\/2)&quot;,&quot;tablespoons: 07&quot;,&quot;cups: 1.043750000 (7\/16)&quot;,&quot;tablespoons: 06&quot;,&quot;cups: 0.375000000 (3\/8)&quot;,&quot;tablespoons: 05.33333&quot;,&quot;cups: 0.333333125 (1\/3)&quot;,&quot;tablespoons: 05&quot;,&quot;cups: 0.312500000 (5\/16)&quot;,&quot;tablespoons: 04&quot;,&quot;cups: 0.250000000 (1\/4)&quot;,&quot;tablespoons: 03&quot;,&quot;cups: 0.187500000 (3\/16)&quot;,&quot;tablespoons: 02&quot;,&quot;cups: 0.125000000 (1\/8)&quot;,&quot;tablespoons: 01&quot;,&quot;cups: 0.062500000 (1\/16)&quot;]</TablespoonsToCups>


  1. Take the XML output and programmatically clean it up so that it can be put through an XLST transformer, and then displayed on a web page.
  2. Adapt the RESTful Client with JavaFX UI to work with this server. The main thing you would need to do is to change the URI call in the UICalculateCups class of the client. You could also add code to access and display the tablespoons to cups conversion chart.
RESTful JSON Service

One thought on “RESTful JSON Service

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s