user3731315
user3731315

Reputation: 23

How to remove the characters �� from the JSON response using Jmeter?

Below is the JSON Response from the Server, How to remove the characters �� from the below Response using Jmeter

Response : {"_id":"5d56cc5c31acfd001298e863","test_id":"5d56cc593801370012bdb2bb","display_order":"1","question_type":"MULTIPLE CHOICE","isbn":"9780393630749","status":"added","question":{"_id":"5d56cc5c31acfd001298e864","questionId":"5d4262bb56c1d800122fcb48","QuestionTitle":{"key":"","value":"","valueRTF":"","valueHtml":"��������\n

I have written the groovy Script. but it is not removing the char.

def response = prev.getResponseDataAsString(); def var1= response.replaceAll("\�", "");

and I need to use this Var1 in another request.

Upvotes: 0

Views: 1589

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

Most probably you're seeing these question marks due to encoding problems, try setting file.encoding property to UTF-8 in system.properties file and restart JMeter, most probably you will see normal text instead of the question marks.

If for some reason the above hint is not applicable I would recommend replacing the whole valueHtml attribute value using JsonBuilder, the relevant code would be something like:

def builder = new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parse(prev.getResponseData()))
builder.content.question.QuestionTitle.valueHtml = ''
vars.put('Var1', builder.toPrettyString())

As the result you will have the same JSON structure with empty valueHtml attribute.

enter image description here

More information:

Upvotes: 1

SAIR
SAIR

Reputation: 499

As it is a JSON Response, add JSON Extractor Post Processor to the Parent Sampler from where the Response is expected. Extract whole JSON with following settings:

enter image description here

Now, use a JSR223 Sampler, with following code in script area:

String var1 = vars.get("jsonOutput");
log.info("Output: " + var1);
String replaceString=var1.replace('?','-'); // replace with whatever you want to, I am replacing it with '-'
log.info("Output: " + replaceString);
vars.put("NewString", replaceString);

Afterwards, you can use ${NewString} wherever you want.

Upvotes: 0

Related Questions