How to extract response body parameters

Hey Guys,

I was wondering if anyone could help me with this.

Katalon script file

Object response = WS.sendRequest(findTestObject('PublicApi/PublicApiEndPoint'))
System.out.println(response.getResponseText())

In the above script file, getResponseText() method spits out below reponse,
how do I extract the value for parameter, lets say “authenticationToken”, “displayName” etc…

{"authenticationToken":"tokenValue",
"displayName":"Service Desk User",
"smartNumber":"+54315645646"}

I don’t see a available method in below link
https://docs.katalon.com/display/KD/Web+Service

Could someone please advise? or
Am I missing something from the above?

Thanks
Niro

1 Like

Niro,

The extracted data from the response in your case is in JSON format, so we could get the value of each key “authenticationToken”, “displayName” and “smartNumber”. Here is an example of geting the value out:

import groovy.json.JsonSlurper class Example {   static void main(String[] args) {      def jsonSlurper = new JsonSlurper()      def object = jsonSlurper.parseText('{ "name": "John", "ID" : "1"}') 		      println(object.name);      println(object.ID);   } }
2 Likes

Hey Trong,

Many thanks above groovy code works like a charm,
however I’ve chose to use Katalon because it supports java, I’ve never worked on Groovy before.
Therefore could you may be point me to a way to achieve this through java way please :slight_smile:

Regards
Niro

Niro,

I found some help from stackoverflow (https://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java). After convert the response to json object, we can get any properties in the json.