0

My jmeter application sends a request and expects the following json in the response.

{"external-profile":{"email":"myemail@gmail.com","firstname":"fn","lastname":"ln","portfolio":{"field1":[],"field2":[],"field3":[]}}}

field1, field2 and field3 could be empty arrays or might have some value. Is there a way to check that the message has field1,2,3 but ignore the value?

I tried doing this but it doesn't work - {"external-profile":{"email":"myemail@gmail.com","firstname":"fn","lastname":"ln","portfolio":{"field1":[\.*],"field2":[\.*],"field3":[\.*]}}}. To be honest, I don't know if this is the right way to specify regular expressions in jmeter.

Manu Chadha
  • 15,555
  • 19
  • 91
  • 184

1 Answers1

0

Since JMeter 5.2 you have JSON JMESPath Assertion which provides keys function, using it you can validate the attributes names, example configuration:

enter image description here

And textual representations for easier copying and pasting:

  • JMESPath: join(',',keys("external-profile".portfolio))
  • Expected value: field1,field2,field3

However it will fail if the order of "fields" will be different so you might want to go for JSON Schema Validator instead, it can be used from JSR223 Assertion

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks. The `json` I mentioned is incomplete. The actual structure is `{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"myemail@gmail.com\",\"firstname\":\"fn\",\"lastname\":\"ln\",\"portfolio\":{\"field1\":[],\"field2\":["value1","value2"],\"field3\":[]}}}"}`. I modified your answer as follows `join(',',keys("additional-info.external-profile".portfolio))` but got error `message:Invalid argument type calling "keys": expected object but was null` – Manu Chadha Dec 04 '20 at 19:31
  • I also tried `join(',',keys("additional-info"."external-profile".portfolio))` but that also gives error `Assertion failure message:Invalid argument type calling "keys": expected object but was null`. The response contains `json` - `{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"myemail@gmail.com\",\"firstname\":\"fn\",\"lastname\":\"ln\",\"portfolio\":{\"field1\":[],\"field2\":[{\"tag\":\"un2-new tag-empty\",\"count\":1},{\"tag\":\"un2-new tag2-empty\",\"count\":1}],\"field3\":[]}}}"}` – Manu Chadha Dec 04 '20 at 20:27
  • I think the problem might be that `additional-info` is a string and not a json object `"additional-info":"{...}"` instead of `"additional-info":{...}`. I need to find a way to convert it to `json` object before I can validate the paths. But I don't know if I can use something like `json.parse` to do this. – Manu Chadha Dec 05 '20 at 13:11
  • I got it working after I converted the string into json object. Your answer is correct w.r.t. the question I had originally created. More details here - https://stackoverflow.com/questions/65151285/jmes-path-assertion-failing-in-jmeter/65167630#65167630 – Manu Chadha Dec 06 '20 at 11:40