HOW TO EXTRACT DATA FROM JSON RESPONSE USING JMETER

转自 https://octoperf.com/blog/2017/03/09/how-to-extract-data-from-json-response-using-jmeter/

People that successfully extract content from Json documents do two things very well:

  • First, they understand how the json format works,
  • Second, they put 100% of their resources execute and scaling Json extraction techniques.

But, you’re probably wondering:

How do JMeter Experts extract relevant content from Json responses?

Here is the secret recipe to mastering Json extraction.

Json is Simple

To get a better understanding of what Json is, here is an example Json document:

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

Json is an extremely simple data format which has taken over XML a few years ago.

You probably ask yourself: why do I need to learn Json?

An increasing number of REST APIs and servers are using Json as their primary data exchange format. At OctoPerf, we are heavily using Json to exchange data between our AngularJS frontend client and our Spring Boot backend.

Want to know the best part?

Since, JMeter 3.0, it’s far easier to extract data from Json responses using the Json variable extractor. In other words, Json extractors are natively available.

JMeter JsonPath Plugin

JMeter JsonPath Extractor Plugin can be downloaded and installed from jmeter-plugins website. As of JMeter 3.0 and above, Json plugin is optional.

Installing JMeter JsonPath Plugin

  • Download plugins-manager.jar and put it into JMETER_HOME/lib/ext directory,
  • Restart JMeter,
  • Click on Options > Plugins Manager in the top menu,
  • Select Available Plugins tab,
  • Select Json Plugins and click on Apply Changes and Restart JMeter.

The JMeter Json Plugin should be available in right click menu Add > Post Processors > Json Path Extractor.

Are you lazy? Because I am. Why not use the native Json Path Extractor instead!

JMeter Json Path Extractor

JMeter’s Json Post Processor uses Json Way, a Java Json Path API, to perform JSon path extraction on server responses.

The Json Path extractor should be placed under an HTTP Sampler. It has several possible settings, hence the most relevant are:

  • Variables Names: semi-colon separate variable names,
  • JSON Path Expressions: self explanatory.
  • Default values: in the case the expression doesn’t apply to the json document being processed.

Awesome! But how do I get started?

Example Json Paths

Here are some example Json Path expressions that can be used to extract data from the Json document exposed above:

JsonPath (click link to try) Result
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[2:] Book number two from tail
$..book[?(@.isbn)] All books with an ISBN number
$.store.book[?(@.price < 10)] All books in store cheaper than 10
$..book[?(@.price <= $[‘expensive’])] All books in store that are not “expensive”
$..book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books

As you can see, it’s easy and flexible to query specific information from a Json document and put them into variables. Let’s explore some of the examples above with JMeter.

Guess what? We’re going to try them out.

Real-life JMeter Examples

Our sample JMX shows how both the JMeter Json Extractor and Plugin JsonPath Extractor work. Before JMeter 3.0, the plugin was required to perform JsonPath extractions. As of JMeter 3.0, there is an integrated support for Json Extractions.

Ready for some action? Let’s go!

Arrays Extraction

Extracting all authors from the store

Extracting Arrays makes possible to extract multiple values from a single Json document at once. For example, we could extract all the authors from the book store:

  • Variable Name: authors
  • JSONPath Expression: $..author

You will get the following variables:

  • authors_1=Nigel Rees
  • authors_2=Evelyn Waugh
  • authors_3=Herman Melville
  • authors_4=J. R. R. Tolkien
  • authors_ALL=Nigel Rees,Evelyn Waugh,Herman Melville,J. R. R. Tolkien (if Compute concatenation checked)
  • authors_matchNr=4

We got all the authors of all the books!

Conditional Extraction

Extracting Book Titles selectively

Suppose now that we want to extract the title of the books whose price is less than or equal to 10:

  • Variable Name: titles
  • JSONPath Expression: $.store.book[?(@.price<= 10)].title

You will get the following variables:

  • titles_1=Sayings of the Century
  • titles_2=Moby Dick
  • titles_matchNr=2

Title of books priced below 10.

Multiple Extraction

Extracting Both Book Author and Title

Suppose now that we want to extract multiple Json fields at the same time. For example, we would like to query all author and titles:

  • Variable Name: multiple
  • JSONPath Expression: $..[‘author’,‘title’]

You will get the following variables:

  • multiple_1={“title”:“Sayings of the Century”,“author”:“Nigel Rees”}
  • multiple_2={“title”:“Sword of Honour”,“author”:“Evelyn Waugh”}
  • multiple_3={“title”:“Moby Dick”,“author”:“Herman Melville”}
  • multiple_4={“title”:“The Lord of the Rings”,“author”:“J. R. R. Tolkien”}
  • multiple_matchNr=4

Extracting Both Book Author and Title

3 Common Mistakes

Now, you’re probably wondering: what can possibly go wrong?

The 3 common mistakes that should be avoided are:

  • Don’t define multiple variables within a single Json Path extractor: the script may become hard to understand / maintain,
  • Don’t write expressions susceptible to work only on specific json responses, try to stick to the general case,
  • The simpler the solution, the better will be the script maintainability.

Good to know Work-Arounds

Depending on the case, you may use alternate techniques to extract content from a server response.

Regular Expression Extractor

Suppose you have a very simple Json document with the following content and you want all first names:

{
 "name":"Simpsons family",
 "members":[
   {"firstName":"Homer", "lastName":"Simpson"},
   {"firstName":"Marge", "lastName":"Simpson"},
   {"firstName":"Bart", "lastName":"Simpson"}
  ]
}

In this case, the regular expressions extractor may fit well because it’s very simple to write a regular expression.

We have defined the following settings:

  • Regular expression: “firstName”:“(.+?)”,
  • Template: $1$,
  • Match Nr: 3, (we want Bart)
  • Default value: whatever you want in case of error.

JSR223 with External Library

By using the Minimal Json Library, and adding it to JMeter you can do the job of extracting json data from a server response too.

Configuring JMeter With an external Lib

Now create a JSR223 Post processor under the Http Sampler whose server response is a Json document. Select Java language and inspire from the following script:

import com.eclipsesource.json.JsonObject;

String jsonString = prev.getResponseDataAsString();
JsonArray members = Json.parse(jsonString).asObject().get("members").asArray();
vars.put("firstName",String.valueOf(members.get(2).getString("firstName","")));

The code above extract the firstName of the third family member and puts it in a variable.

JSR223 with Groovy

JSR223 PostProcessor has Groovy language support which has built-in JSON support so you won’t have to add any .jars. Example code:

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(prev.getResponseDataAsString());
vars.put("firstName", response.members[2].firstName.toString());

BeanShell Json Extractor

Although the same result can be achieved using the BeanShell post processor, we don’t recommend to do so for performance reason. JSR223 post processors should be used in favor of BeanShell post processors. JSR223 with Groovy is several magnitudes faster than BeanShell.

Configuration is very similar to JSR223.

JMeter Plugins (Json Path Extractor)

Since JMeter 3.0, JMeter Json Extractor Plugin should be abandoned in favor of the built in Json Path extractor. This plugin is still useful if you are using older JMeter versions. (2.13 and below)

Exploring

Json extractors are particularly useful in the following cases:

  • Json REST Apis (trending),
  • OAuth2 authentication mechanisms, which uses Json to send and receive access and refresh tokens,
  • Single Page Web Apps (React or AngularJS are mostly seen) which communicate with JSon REST backends.

Conclusion

There are several ways to extract data from Json document using JMeter. Our favorite one is the built-in Json Extractor. And, best of all, Json extractor is fully supported by OctoPerf!

时间: 2024-08-01 20:13:22

HOW TO EXTRACT DATA FROM JSON RESPONSE USING JMETER的相关文章

Python 使用requests模块发送GET和POST请求的实现代码_python

①GET # -*- coding:utf-8 -*- import requests def get(url, datas=None): response = requests.get(url, params=datas) json = response.json() return json 注:参数datas为json格式 ②POST # -*- coding:utf-8 -*- import requests def post(url, datas=None): response = re

ios-转换后JSON响应获取空值

问题描述 转换后JSON响应获取空值 转换为NSArray后,JSOn响应得到的是空值 JSON Response : JSON Log : [ { "0": "41", "intid": "41", "1": "u8a00u3046", "varfirstname": "u8a00u3046", "2": "test

asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码

 本篇文章主要是对asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码进行了介绍,需要的朋友可以过来参考下,需要对大家有所帮助  代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxJson.aspx.cs" Inherits="AjaxJson" %> <!DOCTYPE html PUBLIC &quo

Json序列化和反序列化方法解析

 本篇文章主要是对Json序列化和反序列化方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助  代码如下:  /// <summary>         /// Json序列化,用于发送到客户端         /// </summary>         public static string ToJsJson(this object item)         {               DataContractJsonSerializer serializer

SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的_jquery

json作为一种轻量级的数据交换格式,在前后台数据交换中占据着非常重要的地位.Json的语法非常简单,采用的是键值对表示形式.JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端程序,也可以从服务器端程序传递json格式的字符串给前端并由前端解释.这个字符串是符合json语法的,而json语法又是 javascript语法的子集,所以javascript很容易解释它,而且

【SpringMVC整合MyBatis】springmvc实现json交互-requestBody和responseBody

json数据交互 1.为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. 2.springmvc进行json交互 (1)请求json.输出json,要求请求的是json串,所以在前端页面中需要将请求的内容转成json,不太方便. (2)请求key/value.输出json.此方法比较常用. 3.环境准备 3.1加载json转的jar包 springmvc中使用jackson的

急!!!!急!!!!!急!!!!!C#后台获取的json前台怎么解析,

问题描述 json如下所示:前台解析获取名称[{name:"cable-evap_M_201203_000.png"},{name:"cable-evap_M_201204_000.png"},{name:"cable-evap_M_201205_000.png"},{name:"cable-evap_W_20120304_000.png"},{name:"cable-evap_W_20120311_000.png&

jquery1.4.4 ajax json 在struts2.2.1中没法封装对象,求解脱

问题描述 才学着用jquery的ajax,用json封装对象在action中找不到,求大神帮看下:js:$("#editViewMethod").click(function(){var ids = "";var flag = 0;var jsonStr = "";$("input[type='checkbox'][name!='checkAll']").each(function(){if($(this).attr(&quo

Asp.net中Json数据的转化,读取与应用

  JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,而且它是基于JavaScript 的. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript 等).这些特性使JSON成为理想的数据交换语言.   Json数据有严格的格式,必须遵守这个格式才可以被解析,主要有两种结构   ①"名/值"对的集合在不同的语言中被理解