Search code examples
jmeterbeanshell

Jmeter - replace value within json record


I'm still new to aspects of jmeter and am trying to solve an issue I'm having.

I'm submitting a json record to an api endpoint. The json contains a number of parameters including the following:

{"StartTime":"1487869700", "QueryEndTime":"12345678910", "Terms":"ThingILookFor", "Reason":"ReasonILook" }

I want to change this to be something like:

{"StartTime":"1487869700", "QueryEndTime":"endTime", "Terms":"ThingILookFor", "Reason":"ReasonILook"

The number string is an epoch value. I want endTime to be a variable I can feed into the query body in the Http and specify at runtime.

My problem is that I need to change that epoch value at the time of query submission to the api to be the current epoch value.

I've tried using a Beanshell, but with no success.

This (probably very poor pseudo code) is what I want to do:

  1. Read in my query body (obtained via a csv data config)
  2. Replace the epoch string with the current epoch timestamp (I'm generating this via a function call in my User Defined Variables)
  3. Submit editted query body via my http call

Obviously, any way to combine some of those steps would be good.


Solution

  • May not be the 'right' solution, but this is what I've done and it works:

    1) Set up a user defined variable currentEpoch

    2) Value of currentEpoch is ${__time(/1000,)} as I require epoch in seconds

    3) Replaced the value I want to replace in my test data (variable is "Query") with "replaceQueryStartTimeWithEpoch"

    4) Set up a beanshell sampler with the following code in it:

    vars.put("Query",vars.get("Query").replace("replaceQueryStartTimeWithEpoch", vars.get("currentEpoch")));
    

    Happily this seems to work for what I need.

    Would be interested to see some more 'elegant' solutions.