cancel
Showing results for 
Search instead for 
Did you mean: 

Script Challenge - Unescape Json Strings

SOLVED
Highlighted
Community Hero

Script Challenge - Unescape Json Strings

I wasn't 100% familiar with JSON strings, or working with events and attaching a groovy script, but after a bit of research I was able to put something together that should work. The challenge was as follows:

 

Unescape JSON Strings
Create a Groovy script, which unescapes JSON strings in the body of a REST API response before the response in shown in the response editor:

Example of original response body:

{
"employee": "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"
}

Example of the corresponding modified response body: 

{
"employee": {
"name": "John",
  "age": 30,
  "city": "New York"
}
}

Here, for the request that has a response that needs to be escaped, we have to add an Event to the project. That event should be of type "RequestFilter.afterRequest" and should contain the following groovy script:

 

def responseContent = context.httpResponse.responseContent; 
context.httpResponse.responseContent = responseContent.replaceAll("\\\\", "");

I believe this should solve the problem presented in the script challenge. I learned something new, too! I'd be eager to see how other people solve it, or if I am even on the right track. It appears to work for me, though. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Script Challenge - Unescape Json Strings

@TanyaGorbunova, there's definitely a world of possibilities that I am just now beginning to understand by hooking into events with groovy scripts. I am going to have to do some more exploring myself though to get a feel for further functionality!




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

5 REPLIES 5
Highlighted
Community Manager

Re: Script Challenge - Unescape Json Strings

Great example Matthew! Thanks for your contribution in API Summer.

I'm sure your script will help many community members. Do you think it's possible to extend its functionality somehow more?

---------
Tanya Gorbunova
SmartBear Community Manager

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Community Hero

Re: Script Challenge - Unescape Json Strings

@TanyaGorbunova, there's definitely a world of possibilities that I am just now beginning to understand by hooking into events with groovy scripts. I am going to have to do some more exploring myself though to get a feel for further functionality!




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Highlighted
Community Manager

Re: Script Challenge - Unescape Json Strings

Awesome! I knew you would like this challenge 🙂

---------
Tanya Gorbunova
SmartBear Community Manager

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
New Contributor

Re: Script Challenge - Unescape Json Strings

@msiadak @TanyaGorbunova hey how to add an Event to the project in soap UI(not in soap UI pro)? I am using open socure soap UI

Highlighted
Community Hero

Re: Script Challenge - Unescape Json Strings

@siva90144: You may want to consider starting your own thread instead of piggy-backing on an unrelated post. That being said, you may also want to consider posting in the Open Source SoapUI forum, since that might get you answers from a knowledgebase more applicable to your problem/question.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors