简体   繁体   中英

Regex JSON response Gatling stress tool

Wanting to capture a variable called scanNumber in the http response loking like this:

{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"profile":{"fullName":"TestFirstName TestMiddleName TestLastName","memberships":[{"name":"UA Gold Partner","number":"123-456-123-123","scanNumber":"123-456-123-123"}]}}

How can I do this with a regular experssion? The tool I am using is Gatling stress tool (with the Scala DSL)

I have tried to do it like this:

.check(jsonPath("""${scanNumber}""").saveAs("scanNr")))

But I get the error:

---- Errors --------------------------------------------------------------------
> Check extractor resolution crashed: No attribute named 'scanNu      5 (100,0%)
mber' is defined

You were close first time.

What you actually want is:

.check(jsonPath("""$..scanNumber""").saveAs("scanNr")))

or possibly:

.check(jsonPath("""$.profile.memberships[0].scanNumber""").saveAs("scanNr")))

Note that this uses jsonPath, not regular expressions. JsonPath should more reliable than regex for this.

Check out the JsonPath spec for more advanced usage.

use this regex to match this in anywhere in json:

/"scanNumber":"[^"]+"/

and if you want to match just happens in structure you said use:

/\{[^{[]+\{[^{[]+\[\{[^{[]*("scanNumber":"[^"]+")/

Since json fields may change its order you should make your regex more tolerant for those changes:

val j = """{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"profile":{"fullName":"TestFirstName TestMiddleName TestLastName","memberships":[{"name":"UA Gold Partner","number":"123-456-123-123","scanNumber":"123-456-123-123"}]}}"""
val scanNumberRegx = """\{.*"memberships":\[\{.*"scanNumber":"([^"]*)".*""".r
val scanNumberRegx(scanNumber) = j

scanNumber //String = 123-456-123-123

This will work even if the json fields will be in different order (but of course keep the structure)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM