jMeter RegEx JSON response

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.


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.

Tags:

Regex

Jmeter