-1

In JMeter, I need to extract some fields (City, Classification, and Chain) from a JSON response:

{
  "StoreCode": "111243",
  "StoreName": "Spencer - Sec 14 Gurgaon",
  "Address1": "Gurgaon-Sector-14",
  "Address2": "NCR",
  "Pin": "110000",
  "City": "NCR",
  "Classification": "Vol 4",
  "Chain": "Spencers",
  "Version": "20281",
  "VisitType": "Weekly"
}

Can it be done using the regular expression extractor? Is there another option?

Kobi
  • 135,331
  • 41
  • 252
  • 292
Saurabh Gupta
  • 107
  • 1
  • 3
  • 10
  • 3
    So what is the problem? Have you tried anything so far?? – Piyush Mar 25 '14 at 06:44
  • I cant fathom how you got this far, and not be able to do what you need... – leppie Mar 25 '14 at 06:46
  • @ Piyush I tried regex to extract the fields. For say, City field I used "City":"(.+?)" but it doesn`t work out. – Saurabh Gupta Mar 25 '14 at 06:46
  • Please Give more information. What you have tried so far. In which language are you trying? – Dipika Mar 25 '14 at 06:47
  • @ Dipika See I`m using a regular expression extractor in Jmeter where I have to extract these following fields from JSON array through the help of regex I need to extract the data. – Saurabh Gupta Mar 25 '14 at 06:51
  • possible duplicate of [Jmeter extracting fields/parsing JSON response](http://stackoverflow.com/questions/18562060/jmeter-extracting-fields-parsing-json-response) – Kobi Mar 25 '14 at 06:54
  • I`m not sure it could be but still I needs to figure out what`s wrong in the regex for City field i.e; "City":"(.+?)" – Saurabh Gupta Mar 25 '14 at 06:57
  • I've edited your question to include all data from the comments - the question should mention JMeter, for example. Please edit the question to add new information. Thanks! – Kobi Mar 25 '14 at 07:03
  • @user3227724 you haven't handled whitespace after ':'. It should be like `"City":\s*"([^"]*)"`, but it will fail if your json would contain escaped quotes or use single quotes instead of double. In most cases you should use correct JSON parser. – keltar Mar 25 '14 at 07:05
  • Thanks Keltar ! You are correct. It`s working correct now. Sure, I`ll take care to use correct JSON parser – Saurabh Gupta Mar 25 '14 at 07:14

3 Answers3

1

If this piece of JSON is the all the response - it makes sense to use Regular Expression Extractor.

If you receive larger or more complicated structure - it's better to use special JSON Path Extractor available through plugin. JSON Path expressions for your JSON response would be something like $.City, $.Chain, etc.

See "Parsing JSON" chapter of Using the XPath Extractor in JMeter guide for more details on JSON Path language and on how to install the plugin.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Very easy with the plugin mentioned. See this for example. Here is link to plugin.

My biggest thing to understand was the flow. In your jmeter test you need to have an httprequest that returns data (in this case json data). So running your test you'd see json in the Response Data tab if you have a View Results Tree listener going. If you have this right click on the HttpRequest you want data from. ADD => Post Processors => jp@gc - JSON Path Extractor. Inside that extractor, you can name it anything you want.

The variable name should be one you already have defined in a User Defined Variables config element. The JSON Path would start with a dollar sign, then a dot, then the name of the key you want the value for from your json. So for me: $.logId the value from ... "logId": 4, ... in my json. It will store the number 4 in my userdefined variable. The default value can be set to something you'd never see like -1 or null or false etc...

BTW you reference your variable in your tests with ${variablename}. If putting into json and its a string do "${variablename}". Hope it helps.

Jason Hughes
  • 748
  • 6
  • 18
0

Lots of the way to find out with the help of regular expression. Sharing one of them.

"City": "(.*)",
"Classification": "(.*)",
"Chain": "(.*)",
rayryeng
  • 102,964
  • 22
  • 184
  • 193