Previously wrote an article extracting JSON data format, this time JMeter read the JSON data format to organize.
For example, the response format of an interface is as follows:
{ "Data" : { "Device_vec" : [ { "Agent_version": "9.7.0.2225", "android_id": "e3d699cf01620531", "Asset_number": "", "description": "89vuwdwfvydejqnaanxm11c72ujdmn", "Device_name": "357568061882002", "Email": "[email protected]", "Encryption_log_count": 0, "Encryption_version": "", "Engine_version": "9.715-1024", "Firewall_log_count": 0, "Firewall_version": "", "group_id": "aa000000-0000-0000-0000-000000000000", "Group_name": "Default", "id": "35756806-1882-0020-0000-000000000000", "IMEI": "357568061882002", "Inactive_reason": 0, "Install_time": 1503642124, "Last_connected_time": 1504742375, "Last_scan_time": 1503642674, "Meid": "", "Noncomp_reason": "", "Os_version": "4.3", "Pattern_version": "2.437.00", "Phone_number": "", "Platform_type": 512, "POLICY_ID": 32, "Policy_name": "", "Security_status": 3, "Status": 3, "user_name": "test1504487508089", "Virus_log_count": 26, "Wtp_log_count": 0 }, { "Agent_version": "2.0.0.1518", "android_id": "", "Asset_number": "", "description": "3DLABTLJ7UQOOIODNAJDRFX1II0PCX", "Device_name": "Michael ' s IPhone", "Email": "[email protected]", "Encryption_log_count": 0, "Encryption_version": "", "Engine_version": "", "Firewall_log_count": 0, "Firewall_version": "", "group_id": "aa000000-0000-0000-0000-000000000000", "Group_name": "Default", "id": "6954500b4f14e50bd20634481ee2c6d9f17b4ee3", "IMEI": "35 445006 267069 9", "Inactive_reason": 0, "Install_time": 1503641446, "Last_connected_time": 1503652862, "Last_scan_time": 1503641477, "Meid": "35445006267069", "Noncomp_reason": "", "Os_version": "10.3.2", "Pattern_version": "", "Phone_number": "", "Platform_type": 1024, "POLICY_ID": 6, "Policy_name": "", "Security_status": 1, "Status": 3, "user_name": "test1504487508089", "Virus_log_count": 0, "Wtp_log_count": 0 } ], "Total_count": 2 }, "Error_code": 1, "Message": "Success", "Timestamp": 1504765848}
The following is the extraction of the agent_version parameter of the Device_vec angle Mark 2.
Method One:jmter JSON plugin, JSON Path Extractor extractor.
The way to process JSON data is to use the JMeter plug-in, which can use Jsonpath to get data at a specific location in the JSON data. Similar to Xpath,jsonpath in XML files, you can manipulate JSON objects using simple expressions. The JSON Path Extractor is an open source plugin that adds a post processor that can copy the Lib file of the plugin to the JMeter Lib directory or through the JMeter UI interface Options-->plugins The manager can download the JSON plugin.
You can use the following jsonpath to describe:
$.data.device_vec[1].agent_version
In JMeter, you only need to open the JSON Path Extractor from the Postprocessor menu and enter the variable name and default value as follows:
Jsonpath expressions are short and easy to read, and can effectively improve the serviceability of test scripts, which are not installed with standard JMeter.
Method Two:jmter regular Expression plug-in , regular expression extraction
JMeter After you install the regular expression plug-in, you can extract the data from the string in a fixed format, and in this case the expression is as follows:
"Agent_version": "(. +?)"
When the expression is used, all strings that obey the expression are returned, but only one expression is our concern. You can use 1 groups as a template ($1$), and 2 will return the second data.
Method Three:jmter beanshell extractor, beanshell extractor
Borrowing from JMeter's features for BeanShell support, BeanShell is a lightweight Java-oriented scripting language. BeanShell Post Processor allows you to work with JSON data using standard Java syntax, as shown in the following:
1. Write a Jsonpath reading code as follows:
ImportJava.util.LinkedHashMap;ImportCom.jayway.jsonpath.JsonPath;Importcom.jayway.jsonpath.Predicate;ImportNet.minidev.json.JSONArray; Public Staticstring Readjson (String json, String jsonPath) {Try{Object value= Jsonpath.read (JSON, JsonPath,NewPredicate[0]); if(ValueinstanceofInteger) { returnvalue.tostring (); }Else if(ValueinstanceofString) { returnvalue.tostring (); }Else if(ValueinstanceofBoolean) { returnvalue.tostring (); }Else if(ValueinstanceofJsonarray) {Jsonarray arr=(Jsonarray) value; if(!arr.isempty ()) {returnarr.tojsonstring ();} return""; }Else if(ValueinstanceofLinkedhashmap) { returnvalue.tostring (); }Else{ returnvalue.tostring (); } }Catch(Exception e) {return"Pathnotfound"; }}
Then use this method to read the JSON data format
2. Import Com.eclipsesource.json method to read JSON directly
Com.eclipsesource.json, the source download path is as follows Https://github.com/ralfstx/minimal-json, just put the source code into a jar package to JMeter Lib/ext directory can be
Com.eclipsesource.json Applicable method: http://static.javadoc.io/com.eclipsesource.minimal-json/minimal-json/0.9.3/com/ Eclipsesource/json/jsonobject.html
3. If it is a cyclic read, it is necessary to calculate the length of the list to do the loop---with Jsonpath read way to recycle (the previous Jsonpath method no longer post)
4. If it is a cyclic read, it is necessary to calculate the length of the list to do the loop---read by the Com.eclipsesource.json API read mode
As follows:
Summarize
This article lists three available methods for extracting data from the return values in JSON format, and regular expressions are very advantageous for fast normalization of data in a simple JSON format. The Jsonpath plugin can be used to create scripts that can be maintained and modified, but requires additional plug-in installation work. The final BeanShell with the JSON library is indeed very detailed and relies on the flexibility of the Java language to be further developed.
JMeter---JSON several read methods, ArrayList Loop read