Reputation: 315
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.
Upvotes: 0
Views: 3117
Reputation: 3605
I have created a screencast for the very subject, if that helps. Check it out.
Upvotes: 0
Reputation: 34536
You can setup:
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:
Upvotes: 2
Reputation: 51711
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
Upvotes: 0