1

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.

  • 1
    You would usually not use [tag:regex] to parse JSON. It might work for your example, but what happens if the JSON is getting prettified? – Andreas Louv Jun 10 '16 at 13:26
  • In addition, in serialized JSON objects the order in which properties are defined might change. For instance you might receive `{"prop1":"val1", "prop2":"val2"}` as well as `{"prop2":"val2", "prop1":"val1"}`, and they would represent the same object. I definitely would not use regex to extract data from JSON. – user2340612 Jun 10 '16 at 13:30

1 Answers1

2

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:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133