且构网

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

如何使用正则表达式提取器在 jmeter 中提取 json 响应数据?

更新时间:2022-11-14 21:03:31

authorizationToken 在响应中的外观是否唯一(是否只出现一次)?

如果是这样,您可以简单地使用 正则表达式提取器添加到返回 json 响应的 HTTP 请求,正则表达式如下:HTTP 请求正则表达式提取器参考名称:authToken正则表达式:"authorizationToken":"(.+?)"模板:$1$比赛编号:1

并将进一步提取的值引用为 ${authToken}.

但是,如果您的情况更复杂,并且在 json 响应中有多个 authorizationToken 出现,并且您必须提取具体的可以使用的,例如BeanShell PostProcessor/BSF PostProcessor 添加到同一个 HTTP 请求以使用 beanshell 代码提取值 + json处理库.

如果不是 - 上面的第一个解决方案应该会有所帮助.

更新:

目前处理 JSON 响应的最舒适方式似乎是自定义 JSON utils 用于 jmeter(JSON 路径断言、JSON 路径提取器、JSON 格式化程序),它们也是 Jmeter 插件.

在这种特殊情况下,您可以使用 JSON 路径提取器.

I am just trying to extract json response data using jmeter but not able to do so.
I am getting something like {"authorizationToken":"abcdef"}.
I am trying to get authorizationToken but not able to get this.

Can anyone help me getting this working?

Is authorizationToken appearance unique in response (does it appear only once)?

If it does you may simply use Regular Expression Extractor added to the HTTP Request which returns json response, with regex like following:

HTTP Request
    Regular Expression Extractor
    Reference Name: authToken
    Regular Expression: "authorizationToken":"(.+?)"
    Template: $1$
    Match No.: 1

and refer further extracted value as ${authToken}.


But if your case is more complicated and there several appearances of authorizationToken in json response and you have to extract concrete one you may use e.g. BeanShell PostProcessor / BSF PostProcessor added to the same HTTP Request to extract value with beanshell code + json processing library.

If not - the first solution above should help.


UPDATE:

At the moment the most comfortable way to process JSON responses seems to be custom JSON utils for jmeter (JSON Path Assertion, JSON Path Extractor, JSON Formatter) which are also part of Jmeter Plugins.

In this particular case you can use JSON Path Extractor.