yukti
yukti

Reputation: 167

Jmeter json path extractor - How to Remove [ ] from extracted value

From

token_id="token_id":"82903430-f9b3-4f4b-9efa-ee1b991cb735"

I am extracting token_id using path extractor $..token_id.

And then using the variable in next post request, but it's showing extra brackets in call

"token_id":["82903430-f9b3-4f4b-9efa-ee1b991cb735"]

Upvotes: 4

Views: 5234

Answers (3)

Rishi Sharma
Rishi Sharma

Reputation: 1

Use getToken_1 as suggested by Dmitri T to remove extra brackets, because in uses array to store the response.

${getToken_1} is the proper Regex to remove the [] brackets from above response.

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168157

I belive that it is caused by changes introduced in Plugins version 1.3.0 where JSONPath extractor support of returning multiple matching values was introduced.

You can work it around using one of 3 below approaches:

  1. You can change your JSONPath Expression to

    $..token_id[0]
    

    So you won't have to remove brackets and quotation marks manually

  2. I believe you have something like:

    getToken=["82903430-f9b3-4f4b-9efa-ee1b991cb735"]
    getToken_1=82903430-f9b3-4f4b-9efa-ee1b991cb735
    getToken_matchNr=1
    

    So just using ${getToken_1} should work like a charm

  3. You can use Beanshell PostProcessor to remove brackets and quotation marks. Add it after the JSONPath Extractor and put the following code into "Script" area:

    String getToken = vars.get("getToken");
    getToken = getToken.replaceAll("\\[\"(.*?)\"\\]", "$1");
    vars.put("getToken",getToken);
    

Upvotes: 6

In JSON, brackets means array of strings, numbers, booleans, objects and arrays.

"token_id" is interpreted like a string array. Example:

"token_id":["82903430-f9b3-4f4b-9efa-ee1b991cb735"]

Make sure to change "token_id" to string.

Upvotes: 0

Related Questions