Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It may be worth to use <a href="http://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting" rel="nofollow noreferrer">BeanShell scripting</a> to process JSON response.</p> <p>So if you need to get ALL the "name/description" pairs from response (for each section) you can do the following:<br> 1. extract all the "name/description" pairs from response in loop;<br> 2. save extracted pairs in csv-file in handy format;<br> 3. read saved pairs from csv-file later in code - using <a href="http://jmeter.apache.org/usermanual/component_reference.html#CSV_Data_Set_Config" rel="nofollow noreferrer">CSV Data Set Config</a> in loop, e.g.<br></p> <p>JSON response processing can be implemented using BeanShell scripting (~ java) + any json-processing library (e.g. <a href="http://repo1.maven.org/maven2/com/metaparadigm/json-rpc/1.0/json-rpc-1.0.jar" rel="nofollow noreferrer">json-rpc-1.0</a>):<br> - either in <a href="http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler" rel="nofollow noreferrer">BeanShell Sampler</a> or in <a href="http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor" rel="nofollow noreferrer">BeanShell PostProcessor</a>;<br> - all the required beanshell libs are currently provided in default jmeter delivery;<br> - to use json-processing library place jar into JMETER_HOME/lib folder. <br><br></p> <p>Schematically it will look like:</p> <ol> <li><p>in case of BeanShell PostProcessor:</p> <pre> Thread Group . . . <b>YOUR HTTP Request</b> BeanShell PostProcessor // added as child . . . </pre></li> <li><p>in case of BeanShell Sampler:</p> <pre> Thread Group . . . <b>YOUR HTTP Request</b> BeanShell Sampler // added separate sampler - after your . . . </pre></li> </ol> <p>In this case there is no difference which one use.</p> <p><img src="https://i.stack.imgur.com/ThHF6.png" alt="bsh-sampler_extract-json-data"></p> <p>You can either put the code itself into the sampler body ("Script" field) or store in external file, as shown below. <br><br></p> <p>Sampler code:</p> <pre><code>import java.io.*; import java.util.*; import org.json.*; import org.apache.jmeter.samplers.SampleResult; ArrayList nodeRefs = new ArrayList(); ArrayList fileNames = new ArrayList(); String extractedList = "extracted.csv"; StringBuilder contents = new StringBuilder(); try { if (ctx.getPreviousResult().getResponseDataAsString().equals("")) { Failure = true; FailureMessage = "ERROR: Response is EMPTY."; throw new Exception("ERROR: Response is EMPTY."); } else { if ((ResponseCode != null) &amp;&amp; (ResponseCode.equals("200") == true)) { SampleResult result = ctx.getPreviousResult(); JSONObject response = new JSONObject(result.getResponseDataAsString()); FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir") + File.separator + extractedList); if (response.has("items")) { JSONArray items = response.getJSONArray("items"); if (items.length() != 0) { for (int i = 0; i &lt; items.length(); i++) { String name = items.getJSONObject(i).getString("name"); String description = items.getJSONObject(i).getString("description"); int list_id = items.getJSONObject(i).getInt("list_id"); if (i != 0) { contents.append("\n"); } contents.append(name).append(",").append(description).append(",").append(list_id); System.out.println("\t " + name + "\t\t" + description + "\t\t" + list_id); } } } byte [] buffer = contents.toString().getBytes(); fos.write(buffer); fos.close(); } else { Failure = true; FailureMessage = "Failed to extract from JSON response."; } } } catch (Exception ex) { IsSuccess = false; log.error(ex.getMessage()); System.err.println(ex.getMessage()); } catch (Throwable thex) { System.err.println(thex.getMessage()); } </code></pre> <p>As well a set of links on this:</p> <ul> <li><a href="http://theworkaholic.blogspot.com/2012/05/json-in-jmeter.html" rel="nofollow noreferrer">JSON in JMeter</a></li> <li><a href="http://www.havecomputerwillcode.com/blog/?p=500" rel="nofollow noreferrer">Processing JSON Responses with JMeter and the BSF Post Processor</a></li> </ul> <hr> <p><strong>Upd. on 08.2017:</strong></p> <p>At the moment JMeter has set of built-in components (merged from 3rd party projects) to handle JSON without scripting:</p> <ul> <li><a href="https://jmeter-plugins.org/wiki/JSONPathExtractor/" rel="nofollow noreferrer">JSON Path Extractor</a> (contributed from <a href="https://github.com/ATLANTBH/jmeter-components/" rel="nofollow noreferrer">ATLANTBH jmeter-components</a> project);</li> <li><a href="http://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor" rel="nofollow noreferrer">JSON Extractor</a> (contributed from <a href="https://ubikloadpack.com/" rel="nofollow noreferrer">UBIK Load Pack</a> since JMeter 3.0) - see answer below.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload