To make this short: I have a json response, and I need to extract some parameters from it. (Using jmeter, Regular Expression Extractor)
json response:
{"success":true,"data":[{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":274253,"iconCls":null,"versionNo":1,"parentId":null,"id":"274253>1>>823132>1","nameId":"823132","name":"***PASTATAS 1 – Upės g. 11, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_1","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":274753,"iconCls":null,"versionNo":1,"parentId":null,"id":"274753>1>>823163>2","nameId":"823163","name":"***PASTATAS 5 – Lvovo 45, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_5","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275104,"iconCls":null,"versionNo":1,"parentId":null,"id":"275104>1>>823180>3","nameId":"823180","name":"***PASTATAS 11 – Ukmergės 20, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_11","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275105,"iconCls":null,"versionNo":1,"parentId":null,"id":"275105>1>>823182>4","nameId":"823182","name":"Administracinis pastatas – Naugarduko 3, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_12","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275106,"iconCls":null,"versionNo":1,"parentId":null,"id":"275106>1>>823184>5","nameId":"823184","name":"***PASTATAS 13 – Grybų 35, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_13","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275630,"iconCls":null,"versionNo":1,"parentId":null,"id":"275630>1>>823760>6","nameId":"823760","name":"Mano pavadinimas 1 – Pilies 1, Vilniaus m.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"OBJ_815895","alokAggrType":"OTHER"}],"messages":null}
prettified json response:
{
"success": true,
"data": [{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 274253,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "274253>1>>823132>1",
"nameId": "823132",
"name": "***PASTATAS 1 – Upės g. 11, Vilnius, Vilniaus m. sav.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "TEST_PASTATAS_1",
"alokAggrType": "OTHER"
},
{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 274753,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "274753>1>>823163>2",
"nameId": "823163",
"name": "***PASTATAS 5 – Lvovo 45, Vilnius, Vilniaus m. sav.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "TEST_PASTATAS_5",
"alokAggrType": "OTHER"
},
{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 275104,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "275104>1>>823180>3",
"nameId": "823180",
"name": "***PASTATAS 11 – Ukmergės 20, Vilnius, Vilniaus m. sav.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "TEST_PASTATAS_11",
"alokAggrType": "OTHER"
},
{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 275105,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "275105>1>>823182>4",
"nameId": "823182",
"name": "Administracinis pastatas – Naugarduko 3, Vilnius, Vilniaus m. sav.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "TEST_PASTATAS_12",
"alokAggrType": "OTHER"
},
{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 275106,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "275106>1>>823184>5",
"nameId": "823184",
"name": "***PASTATAS 13 – Grybų 35, Vilnius, Vilniaus m. sav.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "TEST_PASTATAS_13",
"alokAggrType": "OTHER"
},
{
"valId": null,
"oclOclIdIndexType": null,
"yearno": null,
"docId": 275630,
"iconCls": null,
"versionNo": 1,
"parentId": null,
"id": "275630>1>>823760>6",
"nameId": "823760",
"name": "Mano pavadinimas 1 – Pilies 1, Vilniaus m.",
"menesiai": null,
"ivedimas": null,
"rowTypeIndex": 1,
"duomensTipas": null,
"y": null,
"m1": null,
"m2": null,
"m3": null,
"m4": null,
"m5": null,
"m6": null,
"m7": null,
"m8": null,
"m9": null,
"m10": null,
"m11": null,
"m12": null,
"yo": null,
"m1o": null,
"m2o": null,
"m3o": null,
"m4o": null,
"m5o": null,
"m6o": null,
"m7o": null,
"m8o": null,
"m9o": null,
"m10o": null,
"m11o": null,
"m12o": null,
"avg": null,
"editable": null,
"isParent": true,
"locked": false,
"lockMsg": null,
"icontype": "OBT_364",
"leaf": null,
"dptname": null,
"dpt_dpt_id": null,
"is_all_dpt_data": null,
"attId": null,
"code": "OBJ_815895",
"alokAggrType": "OTHER"
}],
"messages": null
}
I need to extract docId, id, nameId given that I will always know code.
Lets say I have object with code OBJ_815895. This is what I've tried so far (regular expression):
"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":(.+?),"iconCls":null,"versionNo":1,"parentId":null,"id":"(.+?)","nameId":"(.+?)","name":".+?","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"OBJ_815895"
At first I thought this was working, and it was, when I was extracting first object in the list...I admit I'm not that good with regular expressions and I would greatly appreciate any advice you can give me.
[Answer]:
Create regular expression extractor:
Regular Expression Extractor [code]
**Reference Name** : code
**Regular Expression** : "code":"(.+?)"
**Template** : $1$
**Match No.** : -1
Regular Expression Extractor [docId]
**Reference Name** : docId
**Regular Expression** : "docId":(\d+)
**Template** : $1$
**Match No.** : -1
Added groovy script to set parameters:
for(int i = 1; i<vars.get("code_matchNr");i++){
if(vars.get("code_"+i) == vars.get("objectId")){
vars.put("docId",vars.get("docId_"+i));
vars.put("id",vars.get("id_"+i));
vars.put("nameId",vars.get("nameId_"+i));
}
}