2
votes

I'm using the regex in Jmeter 2.8 to extract some values from JSON responses.

The response is like that:

{
    "key": "prod",
    "id": "p2301d",
    "objects": [{
            "id": "102955",
            "key": "member",
            ...
         }], 
    "features":"product_features"
}

I'm trying to get everything except the text between [{....}] with one regex. I've tried this one "key":([^\[\{.*\}\],].+?) but I'm always getting the other values between [{...}] (in this example: member)

Do you have any clue?
Thanks.

3
what programming language? - VladL
i want to use the jmeter regex extractor to do this - ALS
Why regex? Use a proper JSON parser for this! - nhahtdh

3 Answers

2
votes

Suppose you can try to use custom JSON utils for jmeter (JSON Path Assertion, JSON Path Extractor, JSON Formatter) - JSON Path Extractor in this case.

  1. Add ATLANTBH jmeter-components to jmeter: https://github.com/ATLANTBH/jmeter-components#installation-instructions.
  2. Add JSON Path Extractor (from Post Processors components list) as child to the sampler which returns json response you want to process:

    enter image description here

    (I've used Dummy Sampler to emulate your response, you will have your original sampler)

    enter image description here

    Add as many extractors as values your want to extract (3 in this case: "key", "id", "features").

  3. Configure each extractor: define variable name to store extracted value and JSONPath query to extract corresponding value:

    • for "key": $.key
    • for "id": $.id
    • for "features": $.features
  4. Further in script your can refer extracted values using jmeter variables (variable name pointed in JSON Path Extractor settings in "Name" field): e.g. ${jsonKey}, ${jsonID}, ${$.features}.

    enter image description here

Perhaps it may be not the most optimal way but it works.

2
votes

My solution for my problem was to turn the JSON into an object so that i can extract just the value that i want, and not the values in the {...}.

Here you can see my code:

var JSON={"itemType":"prod","id":"p2301d","version":"10","tags":[{"itemType":"member","id":"p2301e"},{"itemType":"other","id":"prod10450"}],"multiPrice":null,"prices":null};

//Transformation into an object: 
obj = eval(JSON );

//write in the Jmeter variable "itemtype", the content of obj.itemType:prod
vars.put("itemtype", obj.itemType);

For more information: http://www.havecomputerwillcode.com/blog/?p=500.

1
votes

A general solution: DEMO

Regex: (\[{\n\s*(?:\s*"\w+"\s*:\s*[^,]+,)+\n\s*}\])

Explanation, you don't consume the spaces that you must correctly, before each line there are spaces and you must consume them before matching, that's why isn't your regex really working. You don't need to scape the { char.