[TIPS] How to retrieve values from nested json structures (Maps)



Once in a while we are testing APIs and there will be a value in the json that we really need to retrieve. Well, using these steps, you can find that value no matter where it is nested in the json structure.

Say you are sending a request and you want to retrieve the response. You’ll need to make sure you have these imports:

import com.kms.katalon.core.testobject.ResponseObject as ResponseObject
import com.kms.katalon.core.testobject.RestRequestObjectBuilder as RestRequestObjectBuilder
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent as HttpTextBodyContent
import groovy.json.JsonSlurper as JsonSlurper

And some code like this:

ResponseObject ro = WS.sendRequest(findTestObject('Test/This is your API Test Object'))
String response = ro.getResponseBodyContent()

So, you’ve retrieved your response body. Now you need to parse it. Then, have a line like this:

Map parsedJson = (Map) new JsonSlurper().parseText(response)

Easy peasy so far right? Sure, and here is where it gets a little more complicated. So, at this point, you will need to put a breakpoint right after the Map parsedJson line.

Then you will want to run the debugger on your code. Click the Debug button in Katalon. image

This will open a window with Variables, Breakpoints and Expressions. Click the Expressions tab. image

In the Expressions tab, click Add a new expression and input what you have after the Map, in this case, it’s parsedJson.


Now, click the Run button in debugging (the one that looks like a bug). Once this starts up, it will stop where the breakpoint is. When it stops, it will map your json.


Now, open up keys. If you have several keys, keep this in mind as you are looking for your value. HINT: if you click on your expression, it will give you the response body content so you can look here for what you need. Then, you can open keys and see the different keys.

In our case here, we have several keys. These align with the values. [0] to [0], [1] to [1], etc. If you are looking for something in the data (in this example it is [0]) you will need to map data. So add a line like this:

Map data = parsedJson.data

Let’s say you have a boolean value you want to retrieve. And this boolean value is in data. You’ll want to write something like this:

boolean thisThing = data["thisThingLivesHere"]

So, in order to find this thing, you had to parse the json and map the data. Inside the data was a field called “thisThingLivesHere” and it has a boolean value. If you want to check that it pulled correctly, you can always println and see what the output is.

But, let’s say you want this email address and it lives in user. You’ll need to map user now (just like data) and then instead of boolean you want something like this:

String email = user["email"]

Easy as that. Any time you want to pull something from the json, parse it, map it, map whatever level it is in, then extract it.

(Thanks to @Russ_Thomas and @Ibus for the help on this!!)

[LINK] How to retrieve values from nested json structures (Maps)

That’s quite a write-up Amanda. Kudos :clap::clap:


nicely done young padawan!