8

I've read a few tutorials on jMeter RegEx extraction but it's not working. I'm using jMeter 2.7.

I have this JSON:

{"address":{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}}

and this RegEx Extractor: "id":(.+?), here's the screenshot of jMeter

enter image description here

For the extraction, I get $new_address_id = 2, instead of 26. Any ideas?

UPDATE 6/26/2012:

Thanks Cylian for your suggestion. It is very helpful. I ended up changing it to: "id":(\d+).

I also did a find a replace in the JMX file for

<stringProp name="RegexExtractor.regex">&quot;id&quot;:(.+?,)</stringProp>

and replace with

<stringProp name="RegexExtractor.regex">&quot;id&quot;:(\d+)</stringProp>

Which made the fix quickly. Thanks!

3 Answers 3

14

You are using .+? which means:

  • . - Match any single character that is not a line break character(default, may be changed using s flag)
  • + - Match preceding character between one and unlimited times
  • ? - As few times as possible (lazy)

So, while it is going to match "id":26 the pattern matches .+? as 2 only instead of 26.

To fix this issue you try something better than this:

  ("id":\d+)\b

means

// ("id":\d+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":\d+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single digit 0..9 «\d+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»

or

  ("id":[^,:]+)\b

means

// ("id":[^,:]+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":[^,:]+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single character NOT present in the list “,:” «[^,:]+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»

or

("id":\S+)\b

means

// ("id":\S+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":\S+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single character that is a “non-whitespace character” «\S+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

12

I suggest you take a look at :

http://jmeter-plugins.org/wiki/JSONPathExtractor/

This section (JSON utils (JSON Path Assertion, JSON Path Extractor, JSON Formatter)) in particular for this case. These are set of jmeter tools developed in my company, they're very useful.

Let's take your case as example. The test case looks like this :

enter image description here

So dummy sample returns response, just like the one you specified :

{"address":{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}}

JSON extraction is very simple :

$.address.id

And there you go no need for fancy regexes. Result is 26 (that is what I see in debug sampler).

Update from question in comments :

If you were to have list of values i.e. :

{"address":[{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}, {"id":6,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}]}

List with 2 address-es , 1 has id 26 and another one 6. The Json Path $.address.id should return both of these ids. I just saw sampler source code and it's not possible to get the count however you can do it by adding another post processor to your sample i.e BSF Sampler and by adding this code :

vars.put("ADDRESS_COUNT", "${__javaScript('${add}'.split('\,').length,)}".toString());

Where ${add} is any variable where you stored results of $.address.id.

8 Comments

ant, this is very interesting. However, for some reason, I wasn't able to get mvn to compile the file. Is there a JAR file that can be downloaded without compiling?
@Dean you're probably using maven 2 to build the project. You should use maven 3, sorry for not mentioning that in the github page I'll change that
@Dean were you able to build ?
Hi ant, sorry, I didn't get a chance to build after the regex worked. I'll look into it when I'm doing some more complex checks
Hi ant, I got OS X Lion and use RailsInstaller. It built it very easily. I like the components. However, I couldn't find more info regarding the various components here: atlantbh.com/jmeter-components. For example, there's nothing on the JSON extraction component.
|
0

the best way to select JSON response in jmeter it is to do something like (?m) "nodeRef": "workspace://SpacesStore/idSpaceStore",\s* "name": "folder_for_testing-1372432881900",

(?m) - means start treat regexp as multi line oriented (\s*) - means any characters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.