0

I'm trying to compile a string like :

val route = "this/is/my/url/${body2.id}"

where body2 is a json response from a previous request.

So I tried to compile this string with :

val routeExpression = (route).el[String]

And I saw on this question : Getting the String out of a Gatling expression

I should do something like :

def getRoute = {
  routeExpression.map { route => route }
}

but this function return a Validation[Expression[String]]. How could I get the string compiled ?

Cheers.

Community
  • 1
  • 1
Juanwolf
  • 840
  • 9
  • 19

2 Answers2

0

Finally the problem was not the one asking. In fact I was saving all the json inside the session. But the problem was gatling expression like ${body2.id} works only if the session attribute is like :

Map(body2 -> Map(id -> 45787))

but mine was

Map(body2 -> "{ id: 45787 }")

so I used Jackson library to parse my json to convert it to a map, and it solved the problem.

EDIT

Just have to specify the type as Stephane Landelle said :

check(jsonPath("$").ofType[Map[String, Any]].saveAs("myJson"))

Juanwolf
  • 840
  • 9
  • 19
0

even it's really an old question, but as i also struggling with this once, i should give my use case about that.

it could be better to transform json to map first(use jackson), then could use it easier.

  def jsonToMap(jsonContent: Expression[String]): Expression[Map[String, Object]] = {
    val mapper = new ObjectMapper() with ScalaObjectMapper
    mapper.registerModule(DefaultScalaModule)
    jsonContent.map { json =>
      mapper.readValue(json, classOf[Map[String, Object]])
    }
  }

  def getRoute(bodyMap: Expression[Map[String, Object]]): Expression[String] = {
    bodyMap.map { body2 =>
      "this/is/my/url/${body2.id}"
    }
  }
Rand Chen
  • 101
  • 4