Pretty print json

Once you have converted a java object to json you may want to output the json to view the results. By default JSON is written without line breaks or spaces to save space and be compact. While debugging, this doesn't help because you are trying to read and make sense out of it. This example will show how to pretty print JSON so that it is easy to read and debug using two popular frameworks GSON and jackson.

Setup

class GitHubMember { 
    
    private String login;
    private String id;
    private String avatar_url;
    private String gravatar_id;
    private String url;
    
    public GitHubMember(String login, String id, String avatar_url,
            String gravatar_id, String url) {
        super();
        this.login = login;
        this.id = id;
        this.avatar_url = avatar_url;
        this.gravatar_id = gravatar_id;
        this.url = url;
    }

    public String getLogin() {
        return login;
    }

    public String getId() {
        return id;
    }

    public String getAvatar_url() {
        return avatar_url;
    }

    public String getGravatar_id() {
        return gravatar_id;
    }

    public String getUrl() {
        return url;
    }
}

GSON

@Test
public void pretty_print_json_gson() {

    String GITHUB_MEMBER_JSON = "[\n  {\n    \"login\": \"octocat\",\n    \"id\": \"1\",\n    "
            + "\"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n    "
            + "\"gravatar_id\": \"somehexcode\",\n    \"url\": "
            + "\"https://api.github.com/users/octocat\"\n  }\n]";

    List<GitHubMember> members = Lists.newArrayList(new GitHubMember(
            "octocat", "1",
            "https://github.com/images/error/octocat_happy.gif",
            "somehexcode", "https://api.github.com/users/octocat"));

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    String prettyJson = gson.toJson(members);

    logger.info(prettyJson);

    assertEquals(GITHUB_MEMBER_JSON, prettyJson);
}

Output

[
  {
    "login": "octocat",
    "id": "1",
    "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    "gravatar_id": "somehexcode",
    "url": "https://api.github.com/users/octocat"
  }
]

Jackson

@Test
public void pretty_print_json_jackson() throws IOException {

    String GITHUB_MEMBER_JSON = "[ {\n  \"login\" : \"octocat\",\n  \"id\" : "
            + "\"1\",\n  \"avatar_url\" : \"https://github.com/images/error/octocat_happy.gif\","
            + "\n  \"gravatar_id\" : \"somehexcode\",\n  \"url\" : "
            + "\"https://api.github.com/users/octocat\"\n} ]";

    List<GitHubMember> members = Lists.newArrayList(new GitHubMember(
            "octocat", "1",
            "https://github.com/images/error/octocat_happy.gif",
            "somehexcode", "https://api.github.com/users/octocat"));

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter writer = objectMapper.writer().with(
            SerializationFeature.INDENT_OUTPUT);

    String prettyJson = writer.writeValueAsString(members);

    logger.info(prettyJson);

    assertEquals(GITHUB_MEMBER_JSON, prettyJson);
}

Output

[ {
  "login" : "octocat",
  "id" : "1",
  "avatar_url" : "https://github.com/images/error/octocat_happy.gif",
  "gravatar_id" : "somehexcode",
  "url" : "https://api.github.com/users/octocat"
} ]