Need to have dynamic dates in JSON Post request

Do you want the original {{date}} in the JSON text respected? or is it OK to replace it with the current system clock + 30 days?

replace away. I’m just using {{date}} as a placeholder for this example

Make a new Test Case and paste the following lines; then run it.

import java.time.LocalDate
import java.time.format.DateTimeFormatter

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

 * respoinding to
 * @author kazurayam

// define a utility function
String fatefulDay() {
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");
	LocalDate theDate =
	return formatter.format(theDate)
// just test it
println "${fatefulDay()}"

String source = '''{
"ApiKey": "not telling",
"Email": "don’t need to know",
"Items": [
"Data": [
"SchemaFieldSlug": "ktln-schm",
"Value": "katalon api"
"Events": [
"eventSlug": "katalon-evt",
"occursOn": "right here, need current date + 30 in yyyy-MM-dd",
"complete": false
JsonSlurper slurper = new JsonSlurper()
def parsed = slurper.parseText(source)

println ">>> printing the source in a pretty JSON format"
println JsonOutput.prettyPrint(JsonOutput.toJson(parsed))

// edit the object
parsed.Items[0].Events[0].occursOn = fatefulDay()

println ">>> printing the modified object"
print JsonOutput.prettyPrint(JsonOutput.toJson(parsed))

You will see in the Console something like:

            "Events": [
                    "complete": false,
                    "eventSlug": "katalon-evt",
                    "occursOn": "2020-01-04"

Ok, misunderstanding. I need that date in order to put it into the json for a Post request… my bad.

I think it is your task. You can do it, can’t you?

Or do you want me to tell you how to change your code? If so, you need to share your code on the whole; and the URL of your target Web App needs to be shared public as well so that anyone can reproduce your problem on their side. I suppose you would be reluctant to disclose the detail.


I don’t know much about WS testing in KS (I test mine via the browser). But managing JSON is something I do.

You probably want to investigate jsonSlurper which will allow you to manage the json string as an object. e.g. myjson.myfield, etc.

Regarding the date manipulation, see if this post helps:

What I posted is my code. That’s the json POST call that I am using. This is less about katalon and more about, in JSON, how do I have something like {{currentDate + 30}} in order to have a date be dynamic so I don’t have to change it every time the date passes. I’m not a programmer, I’m a QA. I just so happen to be a rather technical QA that writes automation in katalon, and at times I can write in Selenium in Java. Your response was rather pedantic and rude. I am asking for assistance, if at all possible, in determining how to write, in json, something like currentDate+30 so that I can do my job easier.

EDIT: I apologize for my snark. I recognize that text can be taken with whatever mood the reader is in. I am highly frustrated and definitely felt that … never mind how I felt. I did not need to respond in this manner and I apologize.

That might help. I may have to create it as a keyword or global variable or some such in order to pull it in. But, I couldn’t figure out how to do that since I’m basically using the POST of the API stuff of katalon in order to POST json to my site. But, I don’t know what I don’t know and I’m either having an extremely hard time explaining or I’m making it harder than it needs to be.

If I break it down like this:

If I were using POSTMAN and not KATALON, I would be making a POST request. In the body of the POST request I need a date. But I need that date to be dynamic, ie currentDate+30. If I were only using POSTMAN, is there a way to do that?

That’s how I test mine (but in the browser). I also test “net effects” - the changes that should appear in the app once a post has been submitted.

Never used it.

You can parameterize your post in Katalon. I’m just not familiar with it.

That’s how I test mine (but in the browser). I also test “net effects” - the changes that should appear in the app once a post has been submitted.

That’s exactly what I’m trying to do. I hit the post, then I go look in the UI to make sure the post is there. The trouble I run into is that any date in the past won’t show in our UI. So I have to change the dates. I was hoping that there was a way to grab the current date and add 30 so that I didn’t have to go into every single place where I’ve hard coded the date into the post and change it. Bc we all know that that rarely works - you always miss one and bang your head trying to find it.

You can parameterize your post in Katalon. I’m just not familiar with it.

Yeah, I saw that too. I’m not familiar with it either so I’m not certain that it will take on the date the way I want it.

I was hoping that someone had tried this too. My own dev team doesn’t know how to do it and actually they tell me it can’t be done because json can’t do math. So, I may very well be stuck, but I was really hoping…

Callout to @hpulsford @Ibus (re API/WebService)

I think there is no way to write in json something like currentDate+30 as JSON is just a format of text, not a programming language. You need to write some Groovy script to modify the HTTP Body as you want.

1 Like

@Amanda_Perkins1 build the needed date as a string variable (see already provided advises) and use SimpleTemplateEngine to build your JSON body as a multi-line string variable from a given template:

ok, but how does that go into the json request? Like how do I pull it in? I know how to use GlobalVariables in my katalon scripting, but does the WS portion of Katalon allow me to bring in this particular string variable into the http body of the POST request?

Well, I thank everyone for their help and putting up with me trying to do something that’s a bit weird.

I found a workaround that will probably be best for all involved.

I created a test data spreadsheet and loaded into my test. Then, I used that as a variable in the POST request. So, if I need to change the date, I’ll just go into the calls and update what row the variable is calling. Seems the quickest and easiest solution. (I kinda wish I figured this out earlier)

Thanks again for everyone’s help

you can parametrize the request object and pass the body as a string variable from the testcase, you lazy padawan :stuck_out_tongue:

I don’t know how to do that, Master Jedi… :stuck_out_tongue:

Hi Amanda,

I think you can handle this like so:

1.) You should be able to add parameter handles to your request body. Using your JSON snippet as an example:

    "occursOn" : "2019-12-25"

You should be able to parameterize the object value just like you would for a normal Test Object:

    "occursOn" : "${occursOn}"

2.) Now we need to generate a date of “today’s date + 1 month” and pass it to the request body. You can accomplish this using Java’s LocalDate class, and pass the value to the WS object from your script like so:

import java.time.LocalDate

String todaysDatePlusOne =
WS.sendRequest(findTestObject('path/to/ws/object', ['occursOn' : todaysDatePlusOne]))

Give this a try and let me know :slight_smile:


Does this need to be in say a test case or can I somehow put it into the Query Parameters of the Request?

I’m assuming by this, you mean the code in 2.) from above. Yes, it would either need to be in the test case, or, even better, if this is something you can see yourself using across multiple scripts, you could create a custom keyword to do it. I can elaborate on this if you’d like.

Query parameters are a separate topic altogether.