JSON Parsing. Cannot cast object with class 'java.lang.Integer' to class 'java.util.Map'

Hi,

I work with json that is REST api response (pretty is about 11000 lines).
I can’t parse it. What is wrong?

response status 200 OK.

Cannot cast object '200' with class 'java.lang.Integer' to class 'java.util.Map'

import com.kms.katalon.core.testobject.RequestObject

import com.kms.katalon.core.testobject.ResponseObject

import com.kms.katalon.core.webservice.verification.WSResponseManager

import groovy.json.JsonSlurper

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()

ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

responseString= response.toString()

JsonSlurper js = new JsonSlurper()

Map m = js.parseText(responseString)

String tracksData = m["tracksData"]

println m.get("tracksData").size()

JSON looks like:

{
    "tracksData": [
        {
            "name": "1",
            "area": "Center",           
        }
    ],
    "avgByArea": [
        {
            "area": "Center",
            "avg_wear_coefficient": 0.004851138365159119
        }
    ],
    
    "wearCoefficientOverTunnelmeter": [
        {
            "tunnelmeter": 22,
            "wear_coefficient": 0.002479905440550177,
            "weartype": "Inspection",
            "area": "Gauge",
            "track_name": "29",
            "from": "2023-01-13T12:30:46.000Z"
        },
        {
            "tunnelmeter": 22,
            "wear_coefficient": 0.005698029177732199,
            "weartype": "Inspection",
            "area": "Gauge",
            "track_name": "28",
            "from": "2023-01-13T12:30:46.000Z"
        }
    ]
}

error:

Verification FAILED.

Reason:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '200' with class 'java.lang.Integer' to class 'java.util.Map'

at WSVerification1702655914901.run(WSVerification1702655914901:15)

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.WSVerificationExecutor.runScript(WSVerificationExecutor.java:180)

at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:174)

at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:157)

at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:149)

at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:131)

at com.kms.katalon.core.main.TestCaseMain.runWSVerificationScript(TestCaseMain.java:150)

at com.kms.katalon.core.main.TestCaseMain$runWSVerificationScript$0.call(Unknown Source)

at TempTestCase1702655910490.run(TempTestCase1702655910490.groovy:25)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

P.S. I need to parse this JSON to calculate wearCoefficientOverTunnelmeter.track_name with defined value

Thank you in advance for your help!

1 Like

I think that, this way your verification script gets the response status code 200, and tries to parse it.
I tried to replicate it and got the same error, the error occures with this line:

Map m = js.parseText(responseString)

if you change ‘Map’ to ‘def’ like this, and print right after:

def m = js.parseText(responseString)
println(m)

No error, but the printed line is 200 and nothing else, I tried with a bad url expecting 500 status code and the printed line was 500.So it is definitely the status code that is being parsed and printed.

I think for further help I would need to know what exactly you want to achieve. What do you mean by calculating, do you mean to verify track_name and compare with a predefined value? Or overwrite it with a predefined value?

@viewxy thank you for your answer.

initially I need to check if FE changes are applied in defined use cases.

a TC is planned:

  1. API call
  2. FE changes
  3. the same API call to compare the target value*

*target value - a number of wearCoefficientOverTunnelmeter.track_name records with defined track_name value
e.g. number of "track_name": "29" objects.

Thank you for your help!

Try the following change:

ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

// responseString= response.toString()

JsonSlurper slurper = new JsonSlurper()

//Map m = slurper.parseText(responseString)

Map m = slurper.parseText(response.getResponseBodyContent())

You should check the javadoc of ResponseObject

https://api-docs.katalon.com/com/kms/katalon/core/testobject/ResponseObject.html

You can read the source of ResponseObject here

1 Like

@kazurayam Thank you for your help. The response is parsed successfully.

But another problem has arisen. It seems because of the response big size.

Verification status is incomplete when I try to interact with parsed response.

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.verification.WSResponseManager
import groovy.json.JsonSlurper


RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

responseString= response.toString()

JsonSlurper slurper = new JsonSlurper()
Map m = slurper.parseText(response.getResponseBodyContent())

def wearCoefficientOverTunnelmeter= m.wearCoefficientOverTunnelmeter
 countWearCoef = 0
for (def p: wearCoefficientOverTunnelmeter) {
	if(wearCoefficientOverTunnelmeter.track_name =='34' ) {
	countWearCoef= countWearCoef +1
}
}
println (countWearCoef)

Could you please advise a solution?

Thank you in advance for your help!

something is wrong with a loop. I need some time to understand.

What do you mean by saying “incomplete”?

In the screenshot I found the output from your code:
1229

It looks just fine. That’s all your script does. It has no “verification” code at all.

So what is “incomplete”? I have no idea.

The screenshot shows this:

This looks strange. You would rather want this, don’t you?

if (wearCoefficientOverTunnelmeter.track_name == '34') {
    countWearCoef = countWearCoef + 1
}

@kazurayam thank you. I’ve fixed this loop.

I’ve added an assertion, but the result of verification is still incomplete (lable in Verification Log)

No exeptions.

code that is executed:

import static org.assertj.core.api.Assertions.*

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager

import groovy.json.JsonSlurper


RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

JsonSlurper slurper = new JsonSlurper()
Map m = slurper.parseText(response.getResponseBodyContent())

/*
def avgByArea= m.avgByArea
def avgByArea_gauge= m.avgByArea[0]
def avgByArea_gauge_area= m.avgByArea[0].area

*/

def wearCoefficientOverTunnelmeter= m.wearCoefficientOverTunnelmeter
 countWearCoef = 0
 countWearCoefAll = 0
 
List<String>  wearTypes = new ArrayList<String>()
List<Date>  dateFrom = new ArrayList<Date>()
 
 println (wearCoefficientOverTunnelmeter[0].track_name)
 println(wearCoefficientOverTunnelmeter.size())
 
 for (int i = 0; i <wearCoefficientOverTunnelmeter.size(); i++) {
 
	 
		countWearCoefAll= countWearCoefAll +1
		if (wearCoefficientOverTunnelmeter[i].weartype != 'Inspection') {
		
	countWearCoef= countWearCoef +1
		
	println(i)
	//println(wearCoefficientOverTunnelmeter[i])
	println(wearCoefficientOverTunnelmeter[i].weartype)
	wearTypes.push(wearCoefficientOverTunnelmeter[i].weartype)
	dateFrom.push(wearCoefficientOverTunnelmeter[i].from)
	}
 }
 

println (countWearCoef)
println (countWearCoefAll)
println (wearTypes)
println (dateFrom)




WS.verifyResponseStatusCode(response, 200)

assertThat(response.getStatusCode()).isEqualTo(200)

Do you expect to see any exception here?

I don’t understand why you expect exception here.

@kazurayam
I confused with INCOMPLETE red label on verification Log tab for this call. Other calls have PASSED green label.

I don’t know what “INCOMPLETE” means at all.