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
.
Since JMeter 5.2 you have JSON JMESPath Assertion which provides keys
function, using it you can validate the attributes names, example configuration:
And textual representations for easier copying and pasting:
join(',',keys("external-profile".portfolio))
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
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 followsjoin(',',keys("additional-info.external-profile".portfolio))
but got errormessage:Invalid argument type calling "keys": expected object but was null
I also tried
join(',',keys("additional-info"."external-profile".portfolio))
but that also gives errorAssertion failure message:Invalid argument type calling "keys": expected object but was null
. The response containsjson
-{"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\":[]}}}"}
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 tojson
object before I can validate the paths. But I don't know if I can use something likejson.parse
to do this.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 - stackoverflow.com/questions/65151285/…