My journal project, JSON and API’s
This weekend’s project was the Google’s IP Geo Location API.
I was actually searching for an API for location. Found several ones. Most of them outdated or not free. But Google had the best one I’ve found so far! I signed up and created my key. That was straight forward. What I did not know until then was that API’s come mainly in JSON format. I have not done the RESTFul API courses yet. I just like playing around, attempting something all on my own. Weekends are for playing, and play I did. It was fun!
This simple screen here is the result.

Note: Weather is a dummy. That is the next task in my weekend list.
The process was as follows…
Instead of wasting my 500 calls of the API to test my code, I decided to copy the string and work directly from it.
This was the string:
The XXX are personal location data. So, keeping that private. The repository is also currently private. But I will make it public without all this personal information and keys when I am done.
String location = "{\\"ip\\":\\"XXXXXXXX\\",\\"type\\":\\"IPv4\\",\\"location\\":{\\"latitude\\":XXXXX,\\"longitude\\":XXXX},\\"postcode\\":\\"XXXX\\",\\"area\\":{\\"code\\":\\"NL-ZH\\",\\"geonameid\\":2743698,\\"name\\":\\"South Holland\\"},\\"asn\\":{\\"number\\":1136,\\"organisation\\":\\"KPN B.V.\\"},\\"city\\":{\\"geonameid\\":2747891,\\"name\\":\\"Rotterdam\\",\\"population\\":598199},\\"continent\\":{\\"geonameid\\":6255148,\\"name\\":\\"Europe\\",\\"code\\":\\"EU\\"},\\"country\\":{\\"geonameid\\":2750405,\\"name\\":\\"Netherlands\\",\\"code\\":\\"NL\\",\\"capital\\":\\"Amsterdam\\",\\"area_size\\":\\"41526.00 sq. km\\",\\"population\\":17231017,\\"phone_code\\":\\"31\\",\\"is_in_eu\\":true,\\"languages\\":{\\"fy\\":\\"West Frisian language\\",\\"nl\\":\\"Flemish\\"},\\"flag\\":{\\"file\\":\\"<https://commons.wikimedia.org/wiki/Special:FilePath/Flag_of_the_Netherlands.svg\\",\\"emoji\\":\\">??\\",\\"unicode\\":\\"U+1F1F3 U+1F1F1\\"}},\\"currency\\":{\\"code\\":\\"EUR\\",\\"name\\":\\"Euro\\"},\\"security\\":{\\"is_tor\\":false,\\"is_proxy\\":false,\\"is_crawler\\":false,\\"is_thread\\":false},\\"time\\":{\\"timezone\\":\\"Europe/Amsterdam\\",\\"time\\":\\"2021-08-14 19:24:09 +0200\\",\\"gtm_offset\\":7200},\\"status\\":\\"success\\"}";
I search for a few ways to gather the data from this JSON. And it was… simple, unto a point.
I first had to format the string to understand what I was looking at. I never really understood these JSON files. I was not expecting to have to learn to work with JSON when I was looking for API’s. That’s the code newbie brain for you! Luckily, there are many tools online to help!
I found this website to see the schema of the string: https://jsoneditoronline.org/
I also found https://www.jsonschema2pojo.org/ in order to create the classes needed. However, this turned out to be a LOT. I had no idea what to do with this. Did I really need them all? I just wanted the city and country name. It took me a few videos, and a few websites and documentation to find out about Annotations! I am growing fonder of them the more I see how much code they save.
All I needed was to add this like to the parent class:
@JsonIgnoreProperties(ignoreUnknown = true)
The hardest part was figuring out how to use the data once it was all set up. I had lots of notes from all the research I did and just tried different things until I finally got it working with this code:
String json = "the json string here";
Location location = new Gson().fromJson(json, Location.class);
String city = location .getCity().getName();
String country = location .getCountry().getName();
After that, this is how it looks so far! I am so happy with the progress and all I’ve been learning so far. This GIF recording is messy. But it is for my own progress recording. I want to look back at this a year from now and see how much I’ve done 😀
