Need to extract only if jsonpath available

Hi,
I need to extract the name value from json path. Always the path wont be available sometimes entity will be blank, so if the entity is blank then i need to pass “”.

.check(jsonPath("$.entity.name").exists.saveAs("name")) but this will fail if I use
.check(jsonPath("$.entity.name"").notExists.saveAs("name")) this will pass only if it is not available.
I couldnt implement the doIfEqualsOrElse in check I am finding difficulties.

Sample with entity value:

{
  "A1": 200,
  "_message": "Success",
  "_entity": 
    {
      "name": "ABC",
      "Place": "Xyz"
}
}

Sample with no entity value:

{
  "A1": 200,
  "_message": "Success",
  "_entity": 
    {
      }
}

Hi,

Did you try optional?

.check(jsonPath("$.entity.name").optional.saveAs("name"))

Hope this helps,

Cheers!

Hi @sbrevet
Currently, am using optional
.check(jsonPath(“$.entity.name”).optional.saveAs(“name”))
But I also need to pass “” if the json path doesn’t find $.entity.name , for which optional doesn’t declare the attribute and the #{name} attribute will be undefined

–A

withDefault => Gatling - Checks

Thanks @sbrevet and @slandelle

.check(jsonPath(“$._entity.name”).withDefault(“""”).optional.saveAs(“name”)) – this code will help me fetch name if available or else default will be “” which solved

But I also need to append/concatenate the names within quotes “abc” which I am doing via below code. Any better solutions with transform and mkstring ?
.exec(session => {session.set(“name”, “"”+session(“name”).as[String]+“"”)})

I think there’s really all you need in the documentation page I provided.