5

I'm testing a web service that returns JSON responses and I'd like to pull multiple values from the response. A typical response would contain multiple values in a list. For example:

{
"name":"@favorites",
"description":"Collection of my favorite places",
"list_id":4894636,
}

A response would contain many sections like the above example.

What I'd like to do in Jmeter is go through the JSON response and pull each section outlined above in a manner that I can tie the returned name and description as one entry to iterate over.

What I've been able to do thus far is return the name value with regular expression extractor ("name":"(.+?)") using the template $1$. I'd like to pull both name and description but can't seem to get it to work. I've tried using a regex "name":"(.+?)","description":"(.+?)" with a template of $1$$2$ without any success.

Does anyone know how I might pull multiple values using regex in this example?

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Dave
  • 1,480
  • 3
  • 16
  • 26
  • I am not a JSON expert, but I would assume that a parser for this thing should exist. – FailedDev Nov 14 '11 at 18:05
  • Nope - there are no JMeter built-ins for parsing JSON – Dave Nov 14 '11 at 18:09
  • 1
    @FailedDev there is now -> chk this out https://github.com/ATLANTBH/jmeter-components/ and http://stackoverflow.com/a/11204000/169277 – ant Jul 06 '12 at 11:51
  • @Dave there is now -> chk this out https://github.com/ATLANTBH/jmeter-components/ and http://stackoverflow.com/a/11204000/169277 so doesn't allow me to put two users in same comment .. – ant Jul 06 '12 at 11:51
  • Alternatively chk. my previous answer with json extractor : http://stackoverflow.com/questions/11060483/jmeter-regex-json-response/11204000#comment14776866_11204000 – ant Jul 06 '12 at 11:52

4 Answers4

3

You can just add (?s) to the regex to avoid line breaks.

E.g: (?s)"name":"(.+?)","description":"(.+?)"

It works for me on assertions.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Yaniv
  • 31
  • 2
1

I am assuming that JMeter uses Java-based regular expressions... This could mean no named capturing groups. Apparently, Java7 now supports them, but that doesn't necessarily mean JMeter would. For JSON that looks like this:

{
"name":"@favorites",
"description":"Collection of my favorite places",
"list_id":4894636,
}

{
"name":"@AnotherThing",
"description":"Something to fill space",
"list_id":0048265,
}

{
"name":"@SomethingElse",
"description":"Something else as an example",
"list_id":9283641,
}

...this expression:

\{\s*"name":"((?:\\"|[^"])*)",\s*"description":"((?:\\"|[^"])*)",(?:\\}|[^}])*}

...should match 3 times, capturing the "name" value into the first capturing group, and the "description" into the second capturing group, similar to the following:

1                 2
---------------   ---------------------------------------
@favorites        Collection of my favorite places
@AnotherThing     Something to fill space
@SomethingElse    Something else as an example

Importantly, this expression supports quote escaping in the value portion (and really even in the identifier name portion as well, so that the Javascript string I said, "What is your name?"! will be stored in JSON as AND parsed correctly as I said, \"What is your name?\"!

Community
  • 1
  • 1
Code Jockey
  • 6,611
  • 6
  • 33
  • 45
1

It may be worth to use BeanShell scripting to process JSON response.

So if you need to get ALL the "name/description" pairs from response (for each section) you can do the following:
1. extract all the "name/description" pairs from response in loop;
2. save extracted pairs in csv-file in handy format;
3. read saved pairs from csv-file later in code - using CSV Data Set Config in loop, e.g.

JSON response processing can be implemented using BeanShell scripting (~ java) + any json-processing library (e.g. json-rpc-1.0):
- either in BeanShell Sampler or in BeanShell PostProcessor;
- all the required beanshell libs are currently provided in default jmeter delivery;
- to use json-processing library place jar into JMETER_HOME/lib folder.

Schematically it will look like:

  1. in case of BeanShell PostProcessor:

    Thread Group
        . . .
        YOUR HTTP Request
            BeanShell PostProcessor    // added as child
        . . .
    
  2. in case of BeanShell Sampler:

    Thread Group
        . . .
        YOUR HTTP Request
        BeanShell Sampler          // added separate sampler - after your
        . . .
    

In this case there is no difference which one use.

bsh-sampler_extract-json-data

You can either put the code itself into the sampler body ("Script" field) or store in external file, as shown below.

Sampler 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) && (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 < 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());
}

As well a set of links on this:


Upd. on 08.2017:

At the moment JMeter has set of built-in components (merged from 3rd party projects) to handle JSON without scripting:

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
0

Using Ubik Load Pack plugin for JMeter which has been donated to JMeter core and is since version 3.0 available as JSON Extractor you can do it this way with following Test Plan:

enter image description here

namesExtractor_ULP_JSON_PostProcessor config:

enter image description here

descriptionExtractor_ULP_JSON_PostProcessor config:

enter image description here

Loop Controller to loop over results:

enter image description here

Counter config:

enter image description here

Debug Sampler showing how to use name and description in one iteration:

enter image description here

And here is what you get for the following JSON:

 [{ "name":"@favorites", "description":"Collection of my favorite places", "list_id": 4894636 }, { "name":"@AnotherThing", "description":"Something to fill space", "list_id": 48265 }, { "name":"@SomethingElse", "description":"Something else as an example", "list_id":9283641 }]

enter image description here

Compared to Beanshell solution:

  • It is more "standard approach"

  • It performs much better than Beanshell code

  • It is more readable

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116