JMeter:从数组响应中提取正则表达式 [英] JMeter : Regex extracting from an array response

查看:42
本文介绍了JMeter:从数组响应中提取正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个长数组的响应中提取给定门牌号码的 addressId.数组响应如下所示(片段):

: : "footprint":null,::类型":空,::addressId":0011442239",: : "streetName":"solitudestr.",::streetNrFirstSuffix":空,::streetNrFirst":空,::streetNrLastSuffix":空,::streetNrLast":空,: : "houseNumber":"25",: : "houseName":null,::城市":斯图加特",::邮政编码":70499",: : "stateOrProvince":null,::国家名称":空,::poBoxNr":空,::poBoxType":空,::注意":空,::地理区域":: : [: : ],::名字":空,::姓氏":空,::标题":空,::地区":BW",::附加信息":空,:   :   特性":: : [: : ],::extAddressId":空,::入口":空,::区":空,::addressLine1":空,::addressLine2":空,::addressLine3":空,::addressLine4":空,::公司名称":空,::联系人姓名":空,::houseNrExt":空,::德比堆栈":假:},:{::足迹":空,::类型":空,::addressId":0011442246",: : "streetName":"solitudestr.",::streetNrFirstSuffix":空,::streetNrFirst":空,::streetNrLastSuffix":空,::streetNrLast":空,: : "houseNumber":"26",: : "houseName":null,::城市":斯图加特",::邮政编码":70499",: : "stateOrProvince":null,::国家名称":空,::poBoxNr":空,::poBoxType":空,::注意":空,::地理区域":: : [: : ],::名字":空,::姓氏":空,::标题":空,::地区":BW",::附加信息":空,:   :   特性":: : [: : ],::extAddressId":空,::入口":空,::区":空,::addressLine1":空,::addressLine2":空,::addressLine3":空,::addressLine4":空,::公司名称":空,::联系人姓名":空,::houseNrExt":空,::德比堆栈":假:},

我在此响应中仅显示 2 个门牌号码作为示例,但原始响应更大.

问:我如何匹配特定 houseNumber 的 adressId(我的 CSV 数据集中有这些 houseNumber)?我可以做一个提取所有 addressId 的正则表达式,但是我必须使用正确的匹配号.在 Jmeter.但是,我不能假设这些顺序在我们测试脚本的不同环境中会保持不变.

解决方案

我建议重新考虑使用正则表达式来处理 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.

这篇关于JMeter:从数组响应中提取正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆