How to parse JSON responses in Katalon Studio

Many people in the Katalon forum have asked about retrieving information from JSON responses and parsing the JSON format in Katalon Studio. In this post, I will show a simple way on how to do so.

A JSON response example

Suppose we have the following JSON response, and we want to parse and retrieve its data:

JsonSlurper

We use this Groovy helper class to parse JSON strings. We need to create a new instance of JsonSlurper and call the JsonSlurper.parseText method. Sample code:

The parsed JSON response is now stored in a variable called parsedJson (in our case, it is the Map data structure, but sometimes it may be something else).

JsonSlurper also provides a couple of JsonSlurper.parse overloading methods which can be used if your JSON input is File, Reader, InputStream, URL other than String. For further information, please refer to the JsonSlurper documentation.

Get a key value

Let’s say you want to get a value of id from the JSON response above. JSON is a structured document, so you can get any element using its absolute path. See this example:

As you can see,  there are two ways how to get it. One is to access Map objects by using the dot notation (.). The other is to use get methods from Map, List and Set as you do in Java.

Basically, the parsedJson variable is a type of Map<String, Map<Object, Object>>.
Then, to get inner Map, you call parsedJson.get(“menu”) – as “menu” is the String key. This method returns the inner Map, on which you can call other get methods until you reach your key.

Verify if a key is present in JSON

If you want to verify if a selected key is present in a JSON response, you can use the similar code as below:

You get all keys from the “menu” object and then check if it contains the key you look for.

Get an array element

Your JSON response may also contain arrays. Like any array in Java or Groovy, you can access an array element using arrayName[index].

For example, we can get the “title” value of the first object in the “actions” array as below

In this example, we access the item with the index of 0, the first item in the array (the index is zero-based).

Get an array element based on some condition

A more usual case is when you want to get the exact array element based on some specific condition. For example, you get the “title” value of an object whose “id” is “Open”. You can do this as below:

I used the for-each loop in this case. This loop checks every item in the array array1 until the condition is met. If so, onlickValue1 is assigned to the item’s title.

JSON data types

The JSON format supports a few data types such as String, number, Boolean, and null. If you are not sure what the data type is, you can just use the keyword def.  

A rule of thumb is that a String value is enclosed in quotes, numbers unquoted (floating point may be present as well) and Boolean true or false. But initializing a variable using def is always a good choice when you are not sure about its type.

Conclusion

This tutorial offers a few basic best practices for working with JSON strings in Katalon Studio. JSON is the most common format returned from API/Web Services. When you perform API testing, you likely have to deal with JSON responses. Hopefully, these basic best practices are useful for your API testing.

Rest API Request Katalon Studio

Read more:

About the author

Marek Melocik is currently working as Test Automation Engineer in Brno, Czech Republic. He has been working in the QA industry since 2014.

Meet Marek at  www.linkedin.com/in/marek-melocik

83% people found this helpful
Related testing tools
Katalon Studio
Best automation solution for software testing team. Completely free!
Katalon Recorder
Ultimate Selenium IDE to record, play, and debug app. Fast and extensible!
Katalon Analytics
Bring you to the next level of test execution reports. Smart and seamless solution!
Scroll up