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
* https://forum.katalon.com/t/need-to-have-dynamic-dates-in-json-post-request/37142
*
* @author kazurayam
*/
// define a utility function
String fatefulDay() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");
LocalDate theDate = LocalDate.now().plusDays(30)
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"
}
]