Replacing json value for timestamp on json feeder

I am using json as a feeder as “.queue()” and have a key “timestamp” in it.
Now while creating the scenario I have below code. I have tried to pass the as EL in json directly or replace with in the .body() but nothing seems to be working for me.
I am sure there is quick trick on this, please share if you know.

Note: I cannot load as json file (and use elfilebody), there are some calculations I am doing on top of this code and json file contains multiple array of information which i am using as iterative payloads.

          .exec { session => session.set("timestamp", System.currentTimeMillis().toString)}
          .exec(http("Performance Test")
            .post("some url")
            .header("Content-Type", "application/json")
//            .body(StringBody("${AOEvent.jsonStringify()}"))
//            .body(StringBody("${AOEvent.jsonStringify()}".replace("${time}", "${timestamp}")))

my sample json file

    "AOEvent": {
      "Event": [
          "trans_id": "1234",
          "LastTransDate": "${timestamp}"

Please advice the best approach.


Gatling Expression Language (Gatling EL) is not magic. Only the Gatling API is aware of it.
When dealing with classic methods (such as String::replace in your sample code), you need to use the Session API yourself.

:arrow_heading_up: This is the reason (your calculation) you need to transform the json yourself.
Either by directly modifying the string, or using an external JSON library.

But I guess that your calculations may be done before (in a session => session function as your 2nd line) before introducing it in a JSON pattern file read by ElFileBody.

Did you read about Pebble templating with Gatling?

Hope this helps,

Note: Gatling Expression Language has a currentTimeMillis() built-in function.

1 Like

Thank you both of you for your inputs.

I have tried elfilebody passing #{currentDate(yyyy-MM-dd’T’HH:mm:ss)} with “.body(ElFileBody(“supplyEvents0.json”))” in scenario. This works, I know. This is a different situation.

My situation current is different. I have bunch of json files, in each json file there are bunch of AOEvents. the size of AOEvent also varies within each file. So what i have done is based on the size of AOEvent per json file, I have created iterators, coz these are unique transactions and cannot be run in circular and has to be in .queue().
Every time i read a single AOEvent, it gives me map and for that I am converting it to .jsonStringify() uisng built-in function.
Now when i read a single AOEvent, I also have to replace a field ‘LastTransDate’ for current date. And for this in my below code you will notice i am creating a session key ‘timestamp’ in the session before iterating the AOEvent block of that file. and then here I have need to replace the value of the object.
I have tried with direct replace() and also by placing value “${timestamp}” in the json tile for key ‘LastTransDate’.

And this is where I need some directions, based on this situation, not some other approach of designing the test. Coz I have already come far long in script, and changing approach will be very difficult and thats how I get file from upstream application, can’t do much to the way i get these files…

Also I don’t need millisecond, i need the whole date in this format ‘#{currentDate(yyyy-MM-dd’T’HH:mm:ss)}’

and thats my struggle.

 val date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
    exec(http("pre-req auth call")
      .header("Content-Type", "application/json")
      .header("Authorization", "Basic 1234")
      .exec(session => session.set("access_token", session("access_token").as[String]))
      .exec { session => session.set("timestamp", date.format(System.currentTimeMillis()))}
      .repeat(size0) {
          .exec(http(" Performance Test")
            .header("Authorization", "Bearer ${access_token}")
            .header("Content-Type", "application/json")
//            .body(ElFileBody("supplyEvents0.json"))
//            .body(StringBody("${AOEvent.jsonStringify()}".replace("${time}", "${timestamp}")))
//            .body(StringBody("${AOEvent.jsonStringify()}".replace("${timestamp}", "bbbbb")))

That’s no possible this way.
The only way is that you pass a function to StringBody and implement everything yourself:

 .body(StringBody { session =>
  val event = session("AOEvent").as[Map[String, Any]
  val body: String = computeStringFromEventAndWhatever(event)

Thank you Stephane for this direction… I will try it out… I have to gain familiarity with this new way… I will explore and keep this group posted.

But this could be a good feature to add to your list. This is complex but imagine if user can handle this in one syntax. It will help companies with complex architectures.