Getting Json Parse exception from Json Array to String

Hi

When I am trying to json parse to String using ‘JsonSlurper’ getting can not cast object exception. Help me how parse below json as a string.

json:
[{
“contractId” : “CW12885”,
“contractStatus” : “Published”,
“version” : 7,
“displayVersion” : “v7”,
“warnings” : null
}]

Script:
File file = new File(“/Users/i339884/Downloads/file”)
String text = FileUtils.readFileToString(file)
println text

   **JsonSlurper slurper = new JsonSlurper()**
   Map parsedJson = slurper.parseText(text)

Exception:
2022-10-27 20:02:00.737 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/New/OpenAPI/PublishContractWorkspace/PublishCW FAILED.
Reason:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘[{contractId=CW12885, contractStatus=Published, version=7, displayVersion=v7, warnings=null}]’ with class ‘java.util.ArrayList’ to class ‘java.util.Map’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map(groovy.json.internal.LazyMap)
at PublishCW.run(PublishCW:221)
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:445)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:436)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:415)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:284)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1666881080250.run(TempTestCase1666881080250.groovy:25)

I need to parse above json array to string.

wrong:

right:

Object parsedJson = slurper.parseText(tex)

@kazurayam
yes If i use
Object parsedJson = slurper.parseText(text) I am not getting above cast exception. But how to validate parsed json? I want to validate each element in the below parsed json(ex: contractId->CW12885, contractStatus->Published…etc)

Script:
JsonSlurper slurper = new JsonSlurper()
Object parsedJson = slurper.parseText(text)
print parsedJson

String idValue2 = parsedJson.get(“contractId”)
print idValue2 → Not returns contractId

Console view :
2022-10-28 10:15:24.805 DEBUG testcase.PublishCW - 8: parsedJson = slurper.parseText(text)
2022-10-28 10:15:24.807 DEBUG testcase.PublishCW - 9: print(parsedJson)
[[contractId:CW12885, contractStatus:Published, version:7, displayVersion:v7, warnings:null]]

Try this:

String idValue2 = parsedJson[0].get(“contractId”)

You should be aware parsedJson is in fact a List object. Therefore you need to do

parsedJson[0]

or

parsedJson.get(0)

@kazurayam

Still I am getting complete object but not specific attribute and value.

Look at your code more carefully.

Try

String idValue2 = parsedJson[0].get("contractId")
println idValue2

Don’t bother with “idValue21” and “idValue22”

Don’t do dum copy & paste what was suggested by others. Think yourself.

@kazurayam

Perfect. It’s working fine
String idValue2 = parsedJson[0].get(“contractId”)
println idValue2

Thanks a lot for solving this problem.