netwire
netwire

Reputation: 7215

jMeter RegEx JSON response

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!

Upvotes: 8

Views: 22587

Answers (3)

ant
ant

Reputation: 22948

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.

Upvotes: 12

Skina
Skina

Reputation: 1

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

Upvotes: 0

Cylian
Cylian

Reputation: 11182

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.

Upvotes: 14

Related Questions