Stanislav Dmitruk Stanislav Dmitruk - 4 months ago 28
JSON Question

JMeter Regex - extracting the exact value from json reponse code

I need to extract value of

"id":
for specific "WarehouseService" type of department from json response:

{"id":21,"active":true,"activities":[{"id":229,"activityId":61,"activityName":"Restack","customFieldIds":[8,10],"separateInvoice":false,"unitId":42,"unitName":"Pallet","locationActivityId":0,"incentiveGroupId":null},{"id":228,"activityId":101,"activityName":"Double Roll Off","customFieldIds":[8,9],"separateInvoice":true,"unitId":45,"unitName":"Put","locationActivityId":0,"incentiveGroupId":null},{"id":227,"activityId":129,"activityName":"Container","customFieldIds":[8,5,9],"separateInvoice":false,"unitId":64,"unitName":"Item","locationActivityId":0,"incentiveGroupId":null},{"id":226,"activityId":298,"activityName":"Truck Wash","customFieldIds":[7,6],"separateInvoice":false,"unitId":44,"unitName":"Bottle","locationActivityId":0,"incentiveGroupId":null},{"id":225,"activityId":299,"activityName":"Fueling","customFieldIds":[4,8],"separateInvoice":false,"unitId":190,"unitName":"Percentage","locationActivityId":0,"incentiveGroupId":null}],"comCheckServiceCharge":30.00,"creditCardServiceCharge":1.00,"locationId":15,"name":"SCORS","nameId":53,"selectionRates":[],"serviceRates":[],"type":"WarehouseServices","unloadingRates":[]},


The value I'm looking for is equals to "21" in this string.
The problem is that there are many other parts in response code that contains the same strings of parameters but with different type of department.
Can you guys help me to write correct regular expression for this case so I could match the exact string that countains "WarehouseService" at the end?
I've tried to use lookarounds but it didn't help.

Answer

I wouldn't recommend using regular expressions to parse JSON. Recently released JMeter 3.0 comes with JSON Path PostProcessor which is designed for working with JSON data. The relevant configuration would be:

  • Variable names: id
  • JSON Path Expressions: $..[?(@.type == 'WarehouseServices')].id
  • other fields may be left as they are

You can use JSON Path Tester mode of the View Results Tree listener to test your JSON Path query against real response without having to re-execute the test.

JSON Path Tester

References:

Comments