Search code examples
jsonregexjmeter

Read Response body and assign into one variable in JMeter


I need to read authId value from below response returned by one of the Http Request item in JMeter and assign into in one variable under JSON extractor . "Should I use $.ApiResponse.data.authId" to make it work ?

The Content type is = > Content-Type: application/json; charset=utf-8

{"IsSuccess":true,"Message":"The one-time password (OTP) was successfully generated and sent.","Code":"ONE_TIME_PASSWORD_SUCCESS","ApiResponse":"{\"statusCode\":200,\"data\":{\"message\":\"OTP sent to your email.\",\"authId\":\"eyJ0eXAiOZWTc5NDAwLCJpYXQiOjE3MjE5Nzg4MDB9.LSQBtt7_cugRrxpA0l91UfwEAtAs0QXrB3dEvnPaNt4\",\"callbacks\":[{\"type\":\"TextOutputCallback\",\"output\":[{\"name\":\"message\",\"value\":\"OTP sent to your email.\"},{\"name\":\"messageType\",\"value\":\"0\"}]},{\"type\":\"ConfirmationCallback\",\"output\":[{\"name\":\"prompt\",\"value\":\"\"},{\"name\":\"messageType\",\"value\":0},{\"name\":\"options\",\"value\":[\"Yes. Continue Verify\",\"No. Resend new OTP\"]},{\"name\":\"optionType\",\"value\":-1},{\"name\":\"defaultOption\",\"value\":1}],\"input\":[{\"name\":\"IDToken2\",\"value\":0}]}],\"code\":\"ONE_TIME_PASSWORD_SUCCESS\",\"messageId\":\"APPOP201\",\"email\":\"ss****[email protected]\",\"csrfToken\":\"EjkS2HYV-snDQ2ddFkW861NNZGiv_ieGBsxTvXAKFd--eCvu6_sY\"},\"message\":\"OTP sent to your email.\"}"}

I tried so many regex to make it work but its not working. Then I got one online suggestion to use JSON_Extractor https://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor


Solution

  • It won't work this way, you have JSON inside JSON so you will need 2 JSON Extractors:

    1. Get ApiResponse attribute value from the response data like:

      enter image description here

    2. Get authId value from the ApiResponse JMeter Variable like:

      enter image description here

    More information: How to Use the JSON Extractor For Testing