Search code examples
jsonjmeterjsr223jmeter-5.0json-extract

Handle escaping characters in JSON


In one of my web requests, I get Response Body data as below


{
"JobId":1528,
"CaseId":61687,
"CaseName":"CaseName_3923",
"FirmId":4175,
"FirmName":"FirmName7922442",
"CaseFirmName":"CaseFirmName7922442",
"LastUpdatedDate":"0001-01-01T00:00:00Z"
}

I need to use this whole body response in the next web request, and for that I want to remove the initial  characters.

Is there any way or setting apply in Jmeter by which I can remove these characters? In fact I tried Json Extractor with the settings below, but this is not working, so I assume that the initial  character is creating a problem in not assigning the value of job id to variable vJobid

JSON Extractor: Apply To: Main sample only Name of The Cretaed variables: vJobId Json PathExtractor: $.JobID Match No. 1


Solution

  • The strange characters at the beginning of your JSON structure is an wrongly encoded BOM (Byte Order Mark). It seems that you got a UTF-8 value which shows as an ISO-8859-1 encoded string.

    So the first thing to do would be to find the place where you get the encoding wrong and correct that. If that is not an option, you could try to encode the data back to UTF-8 yourself by using a JSR223PostProcessor before your JSON Extractor with the following Groovy code:

    vars.put("correctedResult",
            new String(prev.responseDataAsString.bytes("ISO-8859-1"), "UTF-8"));
    

    This postprocessor will try to convert the ill-encoded string back to UTF-8 and store that result in the JMeter variable correctedResult. Choose JMeter Variable Name to use with a value of correctedResult in the JSON Extractor to use that newly encoded value instead of the original data.

    But clearly, find the reason for the wrong encoding is the better way.