It is my first time to use VTL, and I tried to follow the documentation about string concatenation, but I can’t make it work. Here is the scenario:
I have a payload:
{
"name":{
"firstname": "John",
"lastname": "Doe"
}
}
The result I got was:
{
"fullname": "John"-"Doe"
}
Also, how can I get the current date (UTC) in Velocity?
In my Velocity template, I did:
#set($firstname= $root.name.firstname)
#set($lastname= $root.name.lastname)
#set($fullname= ${firstname}-${lastname})
{
$h.outputFormat("json")
$h.opt("fullname", $fullname, true)
}
The result I want is:
{
"fullname": "John-Doe"
}
Not sure what the
$hobject in your velocity context represents, but the JSON output you are expecting can be achieved without it, with a template like the below:Date:
To access the date within the template, you'd need to have a DateTool object in your velocity context. To add it to the context, do something like the below:
Once the DateTool object is present in the context, just
$datewould give you the date/time in the current time zone, in a format likeFeb 10, 2024, 7:46:11 PMand$date.getDate().toInstant()would give you the date/time in UTC, in the format2024-02-11T01:46:11.721Z