0
votes

Suppose I have this JSON response:

    [
     {
      "id": "15",
      "userId": "1",
      "new": "true",
      "date": "08/12/2013",
      "text": "…"
     },
     {
      "id": "16",
      "userId": "1",
      "new": "false",
      "date": "08/12/2013",
      "text": "…"
     }
    ]

The regular expresión extractor for the id of every object would have the following configuration:

    Reference name: object
    Regular Expression: "id":"(.+?)"
    Template: $1$
    Match No: -1
    Default value: null

What I need is to extract both id and new from each object, to use them together in a ForEach controller. I need some help with the regular expression for this case.

    Reference name: object
    Regular Expression: ¿? "id":"(.+?)" ¿? "new":"(.+?)" ¿?
    Template: $1$$2$
    Match No: -1
    Default value: null

Also, how do I reference each specific part of the object varible? ${object_1} and ${object_2}?

Edit: sorry, I forgot to mention I´m specificaly using Jmeter.

3

3 Answers

2
votes

You can setup:

enter image description here

enter image description here

And then use either a ForEach Controller or if you only need some values then you will have:

id_1=15

id_2=16 ... new_1=true

new_2=false ...

Indexes are related.

Otherwise you can have a look at this:

0
votes

This is JSON. So, why not parse it as JSON then.
It's quite simple this way. I've used the org.json Java parser.

String jsonData = "[\r\n" + 
        "        {\r\n" + 
        "         \"id\": \"15\",\r\n" + 
        "         \"userId\": \"1\",\r\n" + 
        "         \"new\": \"true\",\r\n" + 
        "         \"date\": \"08/12/2013\",\r\n" + 
        "         \"text\": \"…\"\r\n" + 
        "        },\r\n" + 
        "        {\r\n" + 
        "         \"id\": \"16\",\r\n" + 
        "         \"userId\": \"1\",\r\n" + 
        "         \"new\": \"false\",\r\n" + 
        "         \"date\": \"08/12/2013\",\r\n" + 
        "         \"text\": \"…\"\r\n" + 
        "        }\r\n" + 
        "       ]";

JSONArray jsonRoot = new JSONArray(jsonData);

for (int i = 0; i < jsonRoot.length(); i++) {
    JSONObject jsonObj = jsonRoot.getJSONObject(i);
    System.out.println("Object_" + (i+1) + " = id: " + jsonObj.getString("id") +
            ", new: " + jsonObj.getString("new"));

Output :

Object_1 = id: 15, new: true
Object_2 = id: 16, new: false