Day 52 (week 11) — Consuming JSON

Everything is easier, once you know the answer. That statement is most definitely true today. Continuing to learn about the Spring Framework, today we looked at how to make an API request and parse the JSON response into objects. For our API, we’ll be using the IMDB movie database. The query looked like this:

https://api.themoviedb.org/3/movie/now_playing?api_key=<yourAPIkey>

I’ve left the api_key blank, but you can easily register with the site for free and secure your own key.

Looking at the response we get back,

{
"results": [
{
"vote_count": 1148,
"id": 346364,
"video": false,
"vote_average": 7.5,
"title": "It",
"popularity": 800.186588,
"poster_path": "/9E2y5Q7WlCVNEhP5GiVTjhEhx1o.jpg",
"original_language": "en",
"original_title": "It",
"genre_ids": [
12,
18,
27
],
"backdrop_path": "/tcheoA2nPATCm2vvXw2hVQoaEFD.jpg",
"adult": false,
"overview": "In a small town in Maine, seven children known as The Losers Club come face to face with life problems, bullies and a monster that takes the shape of a clown called Pennywise.",
"release_date": "2017-09-05"
},
{}, {}, {}, ..., other movie objects,
"page": 1,
"total_results": 958,
"dates": {
"maximum": "2017-09-25",
"minimum": "2017-08-07"
},
"total_pages": 48
}

Now the trick is going to be getting Java to parse this response. In Node/Express, this task is remarkably simple. In Java, now that I know how to do it, I suppose it isn’t that bad. But today require some extensive documentation reading and studying.

The magic is recognizing that the response you receive is just one JS object with a property of results that contains the list of movies. And so, the call to fetch the JSON data:

public static List<Movie> getMovies() {

IMDBResponse response = new IMDBResponse();
List<Movie> movies;
RestTemplate restTemplate = new RestTemplate();

try {
response =
restTemplate.getForObject(API_URL, IMDBResponse.class);

} catch( RestClientException ex) {
System.out.println(ex);
}

movies = response.getResults();

return movies;
}

The key to all of this is putting together two classes: IMDBResponse, which models the top-level of the JSON-response and then a separate Movie class that holds each movie’s data.

Then, when we make the call to restTemplate.getForObject we can pass the IMDBResponse.class and Java will know how to parse the response JSON.

The really bad thing about all of this is that if you pass in a class that doesn’t match the JSON response, you get the very cryptic message: Can't parse HTTP response which is entirely unhelpful.

It took me quite some time to figure this out, but I did finally get it. Once I had conquered this problem, the rest of the project was fairly straightforward. I particularly enjoyed the mashup endpoint we created which took all of the sentences from each movie’s summary and mixed them up.

For example. here’s the mashup description for the recent Stephen King movie “It”

A young tech worker takes a job at a powerful Internet corporation, quickly rises up the company's ranks, and soon finds herself in a perilous situation concerning privacy, surveillance and freedom. A young tech worker takes a job at a powerful Internet corporation, quickly rises up the company's ranks, and soon finds herself in a perilous situation concerning privacy, surveillance and freedom. Caesar and his apes are forced into a deadly conflict with an army of humans led by a ruthless Colonel.

It’s just a random smattering of the description from other current movies.

I spent the rest of the day getting ahead and nearly finishing tomorrow’s daily project on JDBC interfaces. Our next step tomorrow will be to look at Postgres database access. I’m fiddling the project tonight to see if I can get it working.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.