How to print the json (pretty) response and also save as json

I have request e.g Login request and now I have to print the response as json and also save as json. how can I do this ? I try something below but never worked any help please.

response= WS.sendRequest(findTestObject(‘Login’))

def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(response.getResponseBodyContent())
def pretty = JsonOutput.prettyPrint(result)
println (pretty)

Please share the execution log. Any error messages there?

2020-02-02 20:34:35.988 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/setcookie FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static groovy.json.JsonOutput.prettyPrint() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[body:[user:[allowedPhoneCodes:[55, 52, 56, 50, 58, 54], company:Sam’s TestCompany | 21 73A St - Dubai - United Arab Emirates | 3rd floor, …], …], …]]
Possible solutions: prettyPrint(java.lang.String)
at setcookie.run(setcookie:36)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1580657662252.run(TempTestCase1580657662252.groovy:23)

hi,

is this imported
import groovy.json.JsonOutput

yeah already imported

hi,

what is print result if used like this way
def result = slurper.parseText(response)
def pretty = JsonOutput.prettyPrint(result)
println (pretty)

not working.

2020-02-02 21:04:35.309 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/setcookie FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (com.kms.katalon.core.testobject.ResponseObject) values: [200 4 KB]
Possible solutions: parseText(java.lang.String), parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader)
at setcookie.run(setcookie:36)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1580659460919.run(TempTestCase1580659460919.groovy:23)

hi,

I have used and no issues
def response = WS.sendRequest(findTestObject(‘WebServiceReqs/webRequest’))
def responseText = response.getResponseText()
println JsonOutput.prettyPrint(responseText)

Katalon version please ?

hello

7.2.1

@Timo_Kuisma1 can I store this pretty json ? why I need this because Im extracting the token and other stuff from login request and set as cookie, so I need proper json format

hi,

yes you can it’s a String
this will return String
JsonOutput.prettyPrint(responseText)

it’s better to extract some values from pure response than String

like here in example
// Parse the response
def restResponse = ‘{“token”: “ewrerydjgkfgijrtupk,nyouktu”,“tokenValidityTime”:“300”,“signInStatus”: “LoginSuccessfull”,“accounts”: [{“accNumber”: “6000112216066021”,“balance”: “0”,“routingNumber”: “122106316”}],“profileInformation”: {“first_name”: “Apple”,“last_name”: “Test”,“sysDate”: “2020-01-30T15:46:40.969Z”}}’
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure
list.each { println it }
String searchToken = “”
for (String s : list){
if (s.startsWith(“token=”)){
searchToken = s
break;
}
}

    println searchToken
    String[] parts = searchToken.split("=");
    String part1 = parts[0]; // 
    String part2 = parts[1]; //
    println part1
    println part2 //here is token ewrerydjgkfgijrtupk,nyouktu

Im doing something like this

def response= WS.sendRequest(findTestObject(‘Login’))
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(response.getBodyContent())

Parse the response and get the userToken value from result
def usertoken = result.body.userToken

Now result is available in usertoken but this is actually not proper json format.

Print JsonOutput.prettyPrint(usertoken) never worked :frowning:

hi,

didn’t got your point
“but this is actually not proper json format”

  1. you are able to get json response?
  2. now you want to get some values from this response
  3. and use those values in a another json request(s) or use as attributes in address to log in some page
  4. How your json request is done and how it’s implemented (java, groovy, if values used in a second request)

Hi @Timo_Kuisma1 well Im not sure whats wrong there and I have try everything to solve it and get the userToken value and set as cookie.
here is my code

def response= WS.sendRequest(findTestObject(‘Login’))
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(response.getResponseBodyContent())

//Get the user token

def usertoken = result.body.userToken

//Set the cookie

Cookie ck = new Cookie(‘userToken’, usertoken)
driver.manage().addCookie(ck)

//Error
2020-02-03 11:02:30.662 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/setcookie FAILED.
Reason:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.openqa.selenium.Cookie(java.lang.String, groovy.json.internal.LazyMap)
at setcookie.run(setcookie:37)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1580709732170.run(TempTestCase1580709732170.groovy:23)

hi,
“Could not find matching constructor for: org.openqa.selenium.Cookie”
check how this constructor is implemented
https://selenium.dev/selenium/docs/api/java/org/openqa/selenium/Cookie.html

hi,

you are passing java.lang.String, groovy.json.internal.LazyMap
and it uses String, String
Cookie(java.lang.String name, java.lang.String value)

@Timo_Kuisma1 after parsing

def result = slurper.parseText(response.getResponseBodyContent())

//Get the user token

def usertoken = result.body.userToken

in usertoken I have string or json object ?

hi,
this “def usertoken” is now groovy.json.internal.LazyMap convert it to the String
maybe this will work
usertoken.toString()

Cookie(java.lang.String name, java.lang.String value)

according to this i have to pass “value” string value. now problem is what im getting in usertoken after parse. And how I can convert it intro string.