0

can somebody help me how to create regex from this? I need only text after a_n: " so I need only this text. e.g. jgoijODIJGojsklfj4dgdg_797gsdg-df_gsdfh-dhfGSDhfdsg-dfg

{
  "a_n": "jgoijODIJGojsklfj4dgdg_797gsdg-df_gsdfh-dhfGSDhfdsg-dfg",
  "type": "something",
  "uuser": "userid",
  "expire": "6018"
}
axiac
  • 68,258
  • 9
  • 99
  • 134
vb381
  • 181
  • 1
  • 3
  • 14
  • 4
    This looks like json. Have you tried using a json parser to load and extract it? –  Sep 29 '17 at 08:34
  • It is response from post request and I need to use this value in next request header. I need it for jmeter so the best way should be regex or xpath.. – vb381 Sep 29 '17 at 08:37
  • regex and xpath are two entirely different things, and both aren't really suited for your use case I would think. Also you should give more context to your question - eg. use case, programming language, how the data will vary. –  Sep 29 '17 at 08:39
  • I have in jmeter POST request and in response data is text that I posted. I need from this text use value of a_n: in next request header. I was using for something similar (xml response) xpath extractor or regex extractor in jmeter. There can be used also some javascript, or beanshell script but I thought it would be easier via regex as I am not very familier with scripts... – vb381 Sep 29 '17 at 08:48
  • Can you see if the answers to this question are helpful: https://stackoverflow.com/questions/18562060/jmeter-extracting-fields-parsing-json-response ? –  Sep 29 '17 at 08:50
  • 1
    Thanks to all. I have installed json extractor plugin to jmeter so I am trying with this, but if can somebody help me also with json it would be great. – vb381 Sep 29 '17 at 09:56
  • Possible duplicate of [Jmeter extracting fields/parsing JSON response](https://stackoverflow.com/questions/18562060/jmeter-extracting-fields-parsing-json-response) –  Sep 29 '17 at 10:33
  • it is working with json extractor, thanks to all `$.a_n` – vb381 Sep 29 '17 at 12:21

5 Answers5

1

You can achieve this with JSONStream.

cat file.json | JSONStream '.a_n'

And also with jq:

jq .a_n file.json
filype
  • 8,034
  • 10
  • 40
  • 66
1

If you can, just use a JSON parser to get the value. Otherwise you can use

/{.*\"a_n\": \"(.*)\".*/
LW001
  • 2,452
  • 6
  • 27
  • 36
1

Please have a look (how to use a regular expression to extract json fields?)

regex should be,

/"a_n":\ ?"((\\"|[^"])*)"/i
0

To extract this in JMeter:

  1. Add a jp@gc - JSON Path Extractor under your request sampler.

  2. To extract the required data, you have to set like this:

enter image description here

  1. Your extracted value will be saved in the variable Jsonvar.

  2. Now You can use this variable other subsequent requests.

Masud Jahan
  • 3,418
  • 2
  • 22
  • 35
0

JMeter's Regular Expression Extractor uses Perl-5 style regular expressions so the relevant configuration would be:

  • Regular Expression: "a_n": "(.+?)"
  • Template: $1$

Also be aware that it makes more sense to use JSON Extractor to work with JSON responses. The relevant JSON Path query would be as simple as $.a_n

Demo:

JMeter JSON Path Extractor

More information: API Testing With JMeter and the JSON Extractor

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