Skip to content

June 30, 2010


Using JSON in your Google App Engine Java app

If you’re using Java in your google app engine web service, you can use google’s GSON library to send JSON responses to your client.  To do that, you must first create a google app engine project. Then, get the GSON library from here:

Find the jar file (e.g. gson-1.4.jar) and place it in the war/WEB-INF/lib folder of your google web project. Now add the file to your build path. If you’re using Eclipse, you would do this by right-clicking on the project, then Build Path->Configure Build Path(Libraries Tab)->Add External JARs. Find the jar file you just added. Press ok and take a look at your project explorer.  You should see your new jar in the list of referenced libraries.

Eclipse project explorer Now you’re ready to use GSON.  I use it inside the class definition of the object I want to convert to JSON. In this example, I am using a class called “Response”, which simply contains two member variables: a status code and a message. But first, at the top of the file, I import the Gson library.


public enum Status {
private Status status;
private String message;

public Response(Status status, String message)
    this.status = status;
    this.message = message;

Then I add the following two methods:

public String toJson() {
    	Gson gson = new Gson();
    	String json = gson.toJson(this);
    	return json;
    public static Response constructFromJson(String json)
    	Gson gson = new Gson();
    	return gson.fromJson(json, Response.class);

Now you can use this in your HttpServlet response. Go to your doGet or doPut function and after doing any necessary processing, you can create and send your response like this:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
           throws IOException
        //do your work here
        PrintWriter out = resp.getWriter();
        Response response = new Response(Response.Status.OK, "A success!");

The response that your client will receive looks like this:  {“status”:”OK”,”message”:”A success”}

*Troubleshooting: Make sure you use print and not println! I accidentally had the println in there from and earlier example, and this added a ‘\n‘ to my JSON string, which confused the parser and created a bug that took forever to find!

Read more from Google App Engine
6 Comments Post a comment
  1. Ken
    Nov 18 2012

    Nice post. Got me up and going THANKS!.

    Perhaps Content-Type: application/json would be better?

  2. Dec 23 2012

    GSON is awesome! I’d been searching for a good JSON library for use in GAE, and I believe my search is over. One important step, which you do mention, but seems to be a common mistake people make when adding libraries like this to their GAE project, is to forget to not only import the external .jar to their project build path, but also to paste the same jars in the war/WEB-INF/lib directory. Without it, things just won’t work, although you’d still get the nice support from your IDE when referencing the library.

  3. Mark
    May 24 2014

    Thanks, good stuff. I am a little confused on how I can access the json that is created though. Is there somewhere I need to specify a directory that it is written to? Also, what should we put in the area where you wrote “your work here”? Thanks, I’m new to web dev, so sorry if this is a dumb question.

  4. Parth
    Aug 19 2014

    The above code gives me a server 500 error. Any ideas how to fix that?

  5. Johne660
    Sep 13 2014

    I’m not sure exactly why but this weblog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later and see if the problem still exists. dkgccgegedad

  6. Guy
    Jan 10 2015

    Thanks for the post! very useful.
    One comment – the constructFromJson didn’t work for me like you wrote. I had to use Type conversion of the class like this –

    return gson.fromJson(json, (Type)Response.class);

Share your thoughts, post a comment.


Note: HTML is allowed. Your email address will never be published.

Subscribe to comments