0

I try to get token in a json to put it in Bearer token to run jmeter, but it does not get the correct content which i want.

Json likes this:

{"access_token":"xxxxxxxxxxxxxxxx","token_type":.......}

I want to get the xxxxxxxxxx only.

I try with regular: \"access_token":"([^\:]*)\", but it returns "access_token":"xxxxxxxxxxxxxxxx".

Please help me to have the correct regular expression. Thanks.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • 1
    That regex might be ok - but you're looking at the whole match rather than the first capturing group. Whatever you're using to do the regex should have a way of accessing capturing groups. As an aside - are you sure there isn't a way to process json natively in your environment? – James Thorpe Oct 30 '19 at 11:16
  • What tool/language are you using? It's better to use a parser. – Toto Oct 30 '19 at 11:22

1 Answers1

1

Regex is the wrong tool for the job! You've already got JSON - it makes much more sense to use a JSON parser. However, if you insist on using regex, the following should work: "access_token":"([^"]*)"

This regex finds the access_token key, and then captures all data up to the first closing ". Note that this will break if " appears inside the data being captured. Also note that " does not need to be escaped in a regex (but may need to be escaped due to the language you're using).

Demo

Nick Reed
  • 4,989
  • 4
  • 17
  • 37