且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JMeter:从数组响应中提取的正则表达式

更新时间:2023-02-18 15:06:53

我建议重新考虑使用正则表达式来处理 JSON 数据.

从 JMeter 3.0 开始,您有一个

您可以使用 JMeter 变量代替硬编码的 25 值,例如:

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

如果由于某种原因你必须使用 JMeter <3.0 你仍然可以使用 JSON Path Extractor 通过 JMeter 插件

参见 JMeter 中 JSON 路径提取器的高级用法 文章,特别是条件选择一章以了解更多信息.

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:

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.