简体   繁体   中英

JMeter : Regex extracting from an array response

I want to extract the addressId for a given housenumber in a response with a long array. The array response looks like this (snippet):

:   :   "footprint":null,
:   :   "type":null,
:   :   "addressId":"0011442239",
:   :   "streetName":"solitudestr.",
:   :   "streetNrFirstSuffix":null,
:   :   "streetNrFirst":null,
:   :   "streetNrLastSuffix":null,
:   :   "streetNrLast":null,
:   :   "houseNumber":"25",
:   :   "houseName":null,
:   :   "city":"stuttgart",
:   :   "postcode":"70499",
:   :   "stateOrProvince":null,
:   :   "countryName":null,
:   :   "poBoxNr":null,
:   :   "poBoxType":null,
:   :   "attention":null,
:   :   "geographicAreas":
:   :   [
:   :   ],
:   :   "firstName":null,
:   :   "lastName":null,
:   :   "title":null,
:   :   "region":"BW",
:   :   "additionalInfo":null,
:   :   "properties":
:   :   [
:   :   ],
:   :   "extAddressId":null,
:   :   "entrance":null,
:   :   "district":null,
:   :   "addressLine1":null,
:   :   "addressLine2":null,
:   :   "addressLine3":null,
:   :   "addressLine4":null,
:   :   "companyName":null,
:   :   "contactName":null,
:   :   "houseNrExt":null,
:   :   "derbyStack":false
:   },
:   {
:   :   "footprint":null,
:   :   "type":null,
:   :   "addressId":"0011442246",
:   :   "streetName":"solitudestr.",
:   :   "streetNrFirstSuffix":null,
:   :   "streetNrFirst":null,
:   :   "streetNrLastSuffix":null,
:   :   "streetNrLast":null,
:   :   "houseNumber":"26",
:   :   "houseName":null,
:   :   "city":"stuttgart",
:   :   "postcode":"70499",
:   :   "stateOrProvince":null,
:   :   "countryName":null,
:   :   "poBoxNr":null,
:   :   "poBoxType":null,
:   :   "attention":null,
:   :   "geographicAreas":
:   :   [
:   :   ],
:   :   "firstName":null,
:   :   "lastName":null,
:   :   "title":null,
:   :   "region":"BW",
:   :   "additionalInfo":null,
:   :   "properties":
:   :   [
:   :   ],
:   :   "extAddressId":null,
:   :   "entrance":null,
:   :   "district":null,
:   :   "addressLine1":null,
:   :   "addressLine2":null,
:   :   "addressLine3":null,
:   :   "addressLine4":null,
:   :   "companyName":null,
:   :   "contactName":null,
:   :   "houseNrExt":null,
:   :   "derbyStack":false
:   },

i only show 2 housenumbers in this response as an example but the original response is bigger.

Q: How can i match the adressId for a specific houseNumber (i have these houseNumbers in my CSV dataset) ? I Could do a regex which extracts all addressId's but then i'd have to use the correct matching no. in Jmeter. However, i cannot assume that the ordening of these will remain same in the different environments we test the script against.

I would recommend reconsidering using regular expressions to deal with JSON data.

Starting from JMeter 3.0 you have a JSON Path PostProcessor . Using it you can execute arbitrary JSONPath queries so extracting the addressID for the given houseNumber would be as simple as:

`$..[?(@.houseNumber == '25')].addressId`

Demo:

JSON路径演示

You can use a JMeter Variable instead of the hard-coded 25 value like:

$..[?(@.houseNumber == '${houseNumber}')].addressId

If for some reason you have to use JMeter < 3.0 you still can have JSON Path postprocessing capabilities using JSON Path Extractor via JMeter Plugins

See Advanced Usage of the JSON Path Extractor in JMeter article, in particular Conditional Select chapter for more information.

You may use a regex that will capture the digits after addressId and before a specific houseNumber if you use an unrolled tempered greedy token (for better efficiency) in between them to make sure the regex engine does not overflow to another record.

"addressId":"(\d+)"(?:[^\n"]*(?:\n(?!: +: +\[)[^\n"]*|"(?!houseNumber")[^\n"]*)*"houseNumber":"25"|$)

See the regex demo (replace 25 with the necessary house number)

Details :

  • "addressId":" - literal string
  • (\\d+) - Group 1 ( $1$ template value) capturing 1+ digits
  • " - a quote
  • (?:[^\\n"]*(?:\\n(?!: +: +\\[)[^\\n"]*|"(?!houseNumber")[^\\n"]*)*"houseNumber":"25"|$) - a non-capturing group with 2 alternatives, one being $ (end of string) or:
    • [^\\n"]* - zero or more chars other than newline and "
    • (?: - then come 2 alternatives:
      • \\n(?!: +: +\\[)[^\\n"]* - a newline not followed with : : [ like string and followed with 0+chars other than a newline and "
      • | - or
      • "(?!houseNumber")[^\\n"]* - a " not followed with houseNumber and followed with 0+chars other than a newline and "
    • )* - than may repeat 0 or more times
  • "houseNumber":"25" - hourse number literal string.

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