Struggling to convert response into JSON Object

Hi there!

I’m having trouble with the snippet “Response Body: Convert to JSON Object.”

I need to convert the following response to a JSON object so I can then verify that the response contains what I expect it to contain.

Here is the response:

{“action”:“find_work_order”,“object”:“find_work_order”,“returnCode”:0,“msgCode”:“AppMsg”,“msgType”:0,“data”:{“enc”:"[{“result”:[{“facility”:“TIG”,“planner”:“CSHEPHER”,“wodescription”:“CSS TESTING SR96003798”,“wopriority”:“02”,“workordernbr”:“00001201”,“workordertype”:“CO”,“wostatus”:“ACTIVE”}]}]"}}

What do I need to add to the Verification to firstly, convert the response, and secondly verify that the facility is TIG.

P.S. when I have tried to verify that facility is set to TIG, it fails as it believes facility is set to ‘null’

Thank you!

Show us what you have done, your code.

If have no idea, please make search in this forum with keyword “ResponseObject JsonSlurper”

By the way, there are a few “JSON Pretty Print” service on the Internet. With it you can check the syntactical validity of any JSON text. For example

I have provided the response body, I just need to know what to put in the verification script to convert the response into JSON object so then I can verify it.

HI, I’ve looked at that but was still unable to convert.

Your JSON that you shared to us looks odd.

The above code should possibly be:

{"enc":[{"result"

Your test successfully detected a problem in the response. Congrat!

Thanks for the response but the JSON object is how it should be and what I expect. The problem is that the response body is a string and I need to convert it into a JSON object.

Okay so this is my response

{
“enc”:"[{“config”:{“listobj”:[{“text1”:“UPPER”,“text2”:“lower”,“text3”:“REPLACE1234”},{“text1”:“UPPER1”,“text2”:“lower1”,“text3”:“REPLACEXX1234”}],“text1”:“UPPER”,“text2”:“REPLACE1234”,“text3”:“lower”,“text4”:“1234567890”,“text5”:“1234”}}]"
}

Katalon believes enc’s value is ‘null’. I need a way to fix this.

Your response is wrong. It is not a valid JSON. I already told you what’s wrong.

Okay here is what is in my verification tab.

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

import internal.GlobalVariable as GlobalVariable

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

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

Here is the response:
{
“enc”:"[{“config”:{“listobj”:[{“listobj1”:[{“text1”:“UPPERTHIS”,“text3”:“lowerthis”},{“text1”:“UPPERTHIS12”,“text3”:“lowerthis12”}],“text1”:“UPPER1”,“text2”:“lower1”,“text3”:“REPLACEXX1234”}],“text1”:“UPPER”,“text2”:“REPLACE1234”,“text3”:“lower”,“text4”:“1234567890”,“text5”:“1234”}}]"
}

How do I verify that text1 is set to UPPERTHIS (the response must be converted to a JSON object first)

I have to unescape the JSON, is there a function for this?

I am saying, the response you got is not a valid JSON.

This is not a valid JSON.

{"enc": "[{"config":"foo"}]"}

The following is a valid JSON.

{"enc": "[{\"config\":\"foo\"}]"}

Do you agree with it?

It just needs to be unescaped. Are you familiar with that term? If so, can you please tell me how I do that in Katalon?

No, you can do nothing in Katalon. Your web server application has a bug. You should fix it first.

The Web Server most definitely does not have a bug. You are wrong.

The JSON it is sending just needs to be unescaped. Here is some information on what that is so you are aware in the future. https://onlinejsontools.com/unescape-json

OK. I will quit.

1 Like

I was able to find a solution. The JSON had to be parsed not just once, but twice using JSON slurper. Then I had to define the correct response as what I expected and used the assert function to make sure the second slurped response was equal to the correct response.

I looked at your original post and looked at the “source code” of your post. I found the following code fragment.

{"action":"find_work_order","object":"find_work_order","returnCode":0,"msgCode":"AppMsg","msgType":0,
"data":{"enc":"[{\"result\":[{\"facility\":\"TIG\",\"planner\":\"CSHEPHER\",\"wodescription\":\"CSS TESTING SR96003798\",\"wopriority\":\"02\",\"workordernbr\":\"00001201\",\"workordertype\":\"CO\",\"wostatus\":\"ACTIVE\"}]}]"}}

Well, regrettably enough, I found there are many escape sequences of \" in it.

You did not use Markdown code formatting syntax, so I could not see \".

I want to say to everyone in this forum, when you are to write a post here, please enclose the code fragments in a pair of triple backticks.

Otherwise, backslash character \ will not be rendered appropriately when others see your post. Your problem might be misunderstood due to missing \ characters.