2

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
ALS
  • 163
  • 3
  • 10

3 Answers3

2

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
  • thanks for your reply but i have some issues to install Json path extractor. it seems that some files are not in the repository anymore... Failure to find org.beanshell:bsh:jar:2.0b5 in http://canal-artifactory:8081/artifactory/repo was cached in the local repository. – ALS Jan 10 '13 at 18:07
  • That's strange because I've successfully downloaded project files and built jars several hours ago. You can possibly try agains (ensure that you are using maven 3.x) - or take here archive with already built components and dependencies jars: http://db.tt/2IQYA0l5. – Aliaksandr Belik Jan 10 '13 at 21:45
  • 1
    finally, i've used a BSF Post Processor to do this. I've transformed my json response to an object with the function eval to do this. thanks for your help. – ALS Jan 14 '13 at 17:38
  • Nice that you've dealt it by yourself. [Usually](http://stackoverflow.com/questions/8125896/pulling-multiple-values-from-json-response-using-regex-extractor?rq=1) I use the same solution too - as the most powerful (perhaps, Beanshell PP instead of BSF one but it doesn't matter). – Aliaksandr Belik Jan 14 '13 at 17:48
  • @ALS , you should put your comment in an answer and accept it. And maybe upvote Alies Belik answer as it seems helpful to me :-) – UBIK LOAD PACK Jan 14 '13 at 19:30
2

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
ALS
  • 163
  • 3
  • 10
1

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.

Javier Diaz
  • 1,791
  • 1
  • 17
  • 25
  • thanks but how can you extract the lines except what is in the [{...}], i want to get the text before and after the objects. "key":"prod", "id":"p2301d", "features":"product_features" – ALS Jan 10 '13 at 13:15
  • So, you want to get it finally as single string in form `"key":"prod", "id":"p2301d", "features":"product_features"` - or as extracted into separate variables values of "key", "id" and "features" correspondingly? – Aliaksandr Belik Jan 10 '13 at 15:10
  • it will be better if i can get all the values separately but the thing is that i won't know where my values will be. for example, the meta "key" can be in the first part of the JSON response or in the last part. – ALS Jan 10 '13 at 15:43
  • @ALS please look then onto the answer above with JSON Path Extractor usage. Solution should be suitable for your scenario. – Aliaksandr Belik Jan 10 '13 at 16:35