Search code examples
jsonjmetertrimjsonresponse

Is it possible to trim JSON object in response using Jmeter?


JSON response returns an object with the following value.

2019-03-20T14:51:30.579+0000

I want to ignore the .579+0000 part for my validation. How can I trim it from the actual value so that I get:

2019-03-20T14:51:30


Solution

  • I would recommend parsing the object value as a Date, this way you will have possibility to convert it to whatever format you like.

    Given you have the following JSON response:

    {
      "someObject": "2019-03-20T14:51:30.579+0000"
    }
    

    You can do the transformation as follows:

    1. Add JSR223 PostProcessor as a child of the request which returns the above JSON
    2. Put the following code into "Script" area:

      def originalDate = new groovy.json.JsonSlurper().parse(prev.getResponseData()).someObject
      log.info("Original date: " + originalDate)
      vars.put("myDate", Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSX", originalDate).format("yyyy-MM-dd'T'HH:mm:ss"))
      log.info("Converted date: " + vars.get("myDate"))
      

      you will need to amend this someObject bit with the path to the JSON attribute holding this date. Once done you should be able to access the "trimmed" data as ${myDate} where required.

      enter image description here

    References: