Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jMeter RegEx JSON response

Tags:

regex

jmeter

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!

like image 645
netwire Avatar asked Jun 16 '12 03:06

netwire


People also ask

How to get response in JSON format in JMeter?

Another way is to install a JMeter plug-in which allows to use JSONPath to access specific elements in JSON. Similar to XPath for XML, JSONPath offers a simple expression syntax to navigate through JSON objects. The JSON Path Extractor is an open source plug-in which adds a post processor for JSON responses to JMeter.


2 Answers

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.

like image 96
Cylian Avatar answered Sep 23 '22 08:09

Cylian


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.

like image 27
ant Avatar answered Sep 21 '22 08:09

ant