How to Parse JSON Data from a REST API using simple JSON Library
author By Nilanjana
Blog

How to Parse JSON Data from a REST API using simple JSON Library

author By Nilanjana Apr 15, 2016
How to Parse JSON Data from a REST API using simple JSON Library
Submit guest post

Let’s start by understanding what is a JSON based API. JSON (Java Script Object Notation) is a lightweight data interchange format that is now being used as a profound and efficient way of gathering, collecting or sharing data among applications and interfaces. JSON provides data to its corresponding calling function in key, value pairs. ‘Key’ as in the variable and ‘value’ as in the corresponding value for the variable. The data that is parsed from a JSON API is in the form of object which needs to be converted into their respective data format as acceptable by the system.

 

I won’t go into much details into describing API’s in this blog post( may be in my upcoming one). REST (REpresentational State Transfer) is an architectural style and is an approach to communicate between different modules that is often used in the development of web services. So in this blog I would describe how can you use JAVA to leverage JSON data from a REST API.

Before starting, here is the REST API I am using to parse data into my system JSON-API.

Now what is the use of parsing JSON data from a web service when I can have it in my system already ? The answer to this would be, now a days maximum of the client data is available over the web as it is not prone to data loss.  Moreover clients built around JSON API are able to take advantage of its features around efficiently caching responses, sometimes eliminating network requests entirely. So let’s proceed ahead and I would try to explain the process of parsing the data.

Step-1: 

Pass the desired URL as an object. So that would be likePass the desired URL as an object. So that would be like

Step-2: 

Type cast the URL object into a HttpURLConnection object. The benefit of doing it is that we would be able to harness the properties of the HttpURLConnection class to validate features. For example set the request type or check the status of the response code.Type cast the URL object into a HttpURLConnection object

Step-3: 

Set the request type as in whether the request to the API is a GET request or a POST request. Set the request type as in whether the request to the API is a GET request or a POST request.  

Step-4: 

Open a connection stream to the corresponding API.Open a connection stream to the corresponding API.  

Step-5: 

Get the corresponding response code.Get the corresponding response code.  

Step-6: 

Now I need to perform a check that if response code is not 200 then throw a runtime exception otherwise carry on the rest of the procedure. So the structure would be

 perform a check that if response code is not 200 then throw a runtime exception otherwise carry on the rest of the procedure.

Step-7: 

I have used the method scanner to read each line from the API and fetch the data in string format. Now this part is inside else { } that I mentioned above.

method scanner to read each line from the API and fetch the data in string format.

method scanner to read each line from the API and fetch the data in string format.

So the parsed data would something like this

Now you have all the data with you from the API. But somehow it looks a bit unstructured and definitely rather than the whole data you would need it categorically. So for this you need to parse this data into JSON object. In some cases you need to store the data in JSON array as well.

JAVA by default does not have any inbuilt class or provide any inbuilt class, method to parse and store these data as objects. So, for that you need the classJSONObject (to store the corresponding string data as JSON objects), JSONArray (to hold JSON objects in an array) and JSONParser (to convert string object into JSON objects). For that you will need a package called SimpleJSON. Download the required jar files and configure its class path in the system.

Step-8: 

Declare an instance of the JSONParser


Declare an instance of the JSONParser

Step-9: 

Convert the string objects into JSON objects.

JSONObject jobj = (JSONObject)parse.parse(inline);

So if you view the JSON structure which would be something like this

 Convert the string objects into JSON objects. JSONObject jobj = (JSONObject)parse.parse(inline); So if you view the JSON structure which would be something like this

I would now like to get the corresponding values under the results array. So here how you do it:

Step-10: 

First convert the JSON object into JSONArray object like this

 First convert the JSON object into JSONArray object like this  

Step-11: 

Once the JSON objects are stored in the array, read the corresponding JSONArray objects, convert it to JSON objects again. So you get the elements within the results array. So here is how you do it.
Once the JSON objects are stored in the array, read the corresponding JSONArray objects, convert it to JSON objects again. So you get the elements within the results array. So here is how you do it.

 

Once the JSON objects are stored in the array, read the corresponding JSONArray objects, convert it to JSON objects again. So you get the elements within the results array. So here is how you do it.

Once the JSON objects are stored in the array, read the corresponding JSONArray objects, convert it to JSON objects again. So you get the elements within the results array. So here is how you do it.

Let’s us dig a bit deeper. Suppose I want the components of “address_components”. Here is the JSON structure

So how would I get the components under the address_components array? Follow the same step as above

That's all I have about the subject, for more resources you can lookup the git hub link to help you out with Parsing Data from JSON REST API.

Discussion

Write to us

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms Of Service apply. By submitting this form, you agree to our Privacy Policy.

See how our uniquely collaborative work style, can help you redesign your business.

Contact us