Wednesday, November 19, 2014

JSON/Java Binding

In this blog, I'll show you how to bind JSON data to Java objects.

Let's start with a simple books example. The following JSON file contains a list of books:

{"ibsn":"978-0486400778", "author":"Mark Twain", "title":"The Adventures of Tom Sawyer"},
{"ibsn":"978-0486280615", "author":"Mark Twain", "title":"Adventures of Huckleberry Finn"}
To map this data to a list of Book objects, first define a Book POJO:
package com.noushin.databinding;

public class Book {
    private String ibsn;
    private String title;
    private String author;
    public String getIbsn() {
        return ibsn;
    public void setIbsn(String ibsn) {
        this.ibsn = ibsn;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public String getAuthor() {
        return author;
    public void setAuthor(String author) { = author;
    public String toString() {
        return String.format("Book ibsn: %s, title: %, author: %s ", ibsn, title, author);

I'll be using Jackson for data binding, so add the proper dependency to your pom file:
<project xmlns="" xmlns:xsi=""



And the code to perform the transformation:
package com.noushin.databinding;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class BookService {

    public List<Book> transform(String jsonFileName) throws IOException, URISyntaxException {
        byte[] jsonData = Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(jsonFileName).toURI()));
        ObjectMapper mapper = new ObjectMapper();
        List<Book> books = mapper.readValue(jsonData, new TypeReference<List<Book>>(){});
        System.out.println("Transformed " + + books.size() + " books.");
        return books;

And finally, here is your JUnit testcase to verify the transformation:
package com.noushin.databinding;

import java.util.List;

import org.junit.Test;

import junit.framework.TestCase;

public class BookServiceTest extends TestCase {

    public void testBookService() {
        BookService bookService = new BookService();
        try {
            List<Book> books = bookService.transform("books.json");
            assertTrue(books.get(0).getAuthor().equals("Mark Twain"));
        catch (IOException e) {
        catch (URISyntaxException e) {