Cannot parse response content

Hello, I’m facing a problem, where I can’t parse response body content.

Here is what I use for parsing, that works for another responses but for current response it doesn’t work.

String getContent = get_response.getResponseBodyContent()
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(getContent)

And it gives me a following error:

What is your full script and which line of code caused the error? I am afraid that the error is not on the above script.

This is my full script and then, I use it in test case so:
def k = CustomKeywords.'db_connection.db_swagger_GET_Insurance_data.getResource'()

println(k)
Map parsedJson = slurper.parseText(getContent)

So the response is not a Map. You could use an ArrayList to catch the result.

1 Like

So, I should use ArrayList instead of Map ?

yes, it is

using ArrayList, still getting an error.

This is another error. I think you sould google for how to access Groovy/Java ArrayList data type.

used an ArrayList instead of Map and instead of parsedJson.get("") used this parsedJson[0][“companyName”] and now it is working for now.

Sometime is tricky testing such responses, I’ve been hit by this. Some API’s are responding with JSON object, some with JSON array.
If you know wellyour API, the solution is simple, as above … if not …
Groovy is smart enough to handle this himself, avoid strong typing and just do:

parsedJson = slurper.parseText(getContent)

and check the type after
i am using this trick for JSON schema validation, since if it is an object has to use a keyword, if is array must use a different one :wink:

2 Likes

Thanks for your help and advice ! :slight_smile: