This blog explains how to parse JSON data to be used in analytics testing.
Key Points
Basic understanding of the JSON format.
Need for JSON parsing in analytics testing.
Parsing of the JSON data.
Basic understanding of the JSON Format:
JSON stands for JavaScript Object Notation. Its primarily used to transmit data between servers and web applications.
JSON data is written as name/value pair.
JSON consists of mainly two structured types: Arrays and Objects.
Below is an example of the JSON structure.
Magic Mantra in JSON:
“[….] represents array and {…..} represents Objects. It will make your task so easy while parsing required data from JSON”.
Need for JSON Parsing in Analytics Testing:
In analytics testing, we deal with fired network calls(response of request). When an HTTP request is made, these calls get fired. It fires in the Network tab. Sometimes there is a need to validate any parameter of the response. To fulfill this requirement, we can make use of JSON parsing. You can use it in your automation code. Confused? Let’s understand by referring to some images.
I am taking an e.g. of https://www.trivago.com/, for instance. So you will get a more precise understanding.
Firstly, Hit the site in the browser.
Secondly, Open Network tab (right-click – do inspect – network)
Additionally, set the filter to “API.” After that, the below request is made. (Note: Similarly, you can filter with any keyword as per your project requirement”)
Let’s take a look at its payload response:
When you click on “view source,” you will get the entire JSON response as below:
What if we need only “pg” and “href” values from this stuff? It’s looking messy, Isn’t it? We can beautify the above JSON. Hit https://jsonformatter.org/ in the browser. You simply need to copy the above source code. Paste that code on the left side. As a result, it will automatically beautify it on the right side.
And now you can parse JSON quickly. It is easy to determine arrays and objects directly. Due to this, you will get the parameters with your needed values. Further, you can validate those parameters with the required result. (Using assertion in selenium)
How to Parse JSON data:
After reading this step, you will surely be capable of parsing JSON data.
Objects
Arrays
String parameter
Nested object inside the array.
Below is a dummy JSON data image. It will familiarize you with JSON structured type.
Please Keep Magic Mantra in your mind; open json_parsing_in_java in another tab. And go ahead with the reading.
As much as you go deeper into it, you will realize that we are playing around with only three methods to parse data:
getString
getJSONObject
getJSONArray.
Note: Please refer above image for a good understanding of the below methods:
Common Step:
We have stored our JSON data in String’ JSON. To parse anything from JSON, firstly, you need to create an object of class ‘JSONObject.’ Then pass your JSON data to it.
JSONObject obj = new JSONObject(json);
Parse String Parameter:
Parsing String data is relatively simple because you just need to use the ‘getString’ method and pass the key into it.
String s_media = obj.getString(“social_media”);
Output = instagram
Parse Object:
If you want to parse any object, use the method ‘getJSONObject(key)’.
String pageinfo = obj.getJSONObject(“pageInfo”).toString();
Output={“pagePic”:”http://example.com/content.jpg”, “pageName“: “abc”}
Parse Particular Key from an Object:
Use ‘getString(key-name)’ method.
String pageName=obj.getJSONObject(“pageInfo”).getString(“pageName”);
Output = abc
Parse Array Element:
To parse array element, there is one method available named as ‘getJSONArray()’.
JSONArray arr = obj.getJSONArray(“posts”);
#Note:‘posts’ is array name.
#Then we can iterate through array as below:
For(int i=0;i<arr.length();i++){
String post_id=arr.getJSONObject(i).getString(“post_id”);
System.out.println(post_id);
}
Output = 123456789012_123456789012
Parse Nested Object in Array:
In the above example, the ‘comments’ object is nested within the array. So we cannot use getJSONObject() method directly. We need to convert that object into a String. Then only we can process it. It is an array. Consequently, we need to iterate through it as below.
For(int i=0;i<arr.length();i++){
String separate_obj = arr.getJSONObject(i).get(“comments”).toString();
System.out.println(separate_obj);
}
Output= {“name1″:”Priya”,”name2″:”Sakshi”,”name3″:”Raheem”}
In conclusion, by using the above methods:
You can extract data from JSON.
Store it in Strings.
Validate your data with expected data in selenium Scripts.
This is all about JSON Parsing. I hope you enjoyed this article.
“Happy Coding…”
Leave A Comment