Lightweight Scripting of Datetime Fields in JSON Responder
Is there a simple way to programmatically alter certain values in an otherwise static JSON response payload with Virtualize?
For example, let's say I've defined something like the following as a literal response in a JSON Message Responder:
{ "foo": "some static value for foo", "timestamp": "1970-01-01T00:00:00Z" }
I'd like to alter the timestamp
property whenever the response is given to represent, for example, the present moment in time. In javascript, something like this could be achieved with the following logic:
response.timestamp = new Date().toISOString();
Is there any lightweight facility in Virtualize that provides such a hook?
By "lightweight", I'm especially looking to avoid calling into the Java Extensibility API as I'm having a hard time groking the documentation. However, if it turns out to be the only way to satisfy this use case, I'd certainly appreciate pointers towards using it.
Best Answer
-
Hi Alex,
If you are using version 9.10.2 of Virtualize, then we added a tool called the Data Generator Tool that you can use to generate timestamps in that manner without scripting.
In prior versions of the tool, you will need to script the field to send the current date. If you are using javascript, then the following script should work for you:
function time(){ return new Date().toISOString(); }
Hope this helps!
1
Answers
-
One thing to note about the script - if the performance is not sufficient, writing the script in Java will achieve much better performance. But the Data Generator Tool is the way to go.
1 -
@Thomas Moore Ah. My problem is that I never used a named function. I see now that is what populates the "method" dropdown on the scripting interface. Awesome.
For reference, here's what I tried before posting here:
An anonymous function:function (){ return new Date().toISOString(); }
Returning the value globally from the script (this prints an error making it obvious that there is an
eval()
going on somewhere.return new Date().toISOString();
Since I guessed the script was being
eval
'd, I tried relying on Last Expression Evaluatedvar timestamp = new Date().toISOString();
Obviously, none of these worked.
Maybe I just missed it, but I felt like the documentation was lacking in how to accomplish this.
Also, I saw this in the docs:
The Virtualize JavaScript emulation is based on FESI. For details on FESI, visit http://home.worldcom.ch/~jmlugrin/fesi/
- http://home.worldcom.ch/~jmlugrin/fesi/ is a dead link
- Honestly, for 2017 this implementation seems outdated. In fact,
toISOString()
is not even available onDate
. I will have to polyfill it to achieve my goal. Although, that's proving not to be straightforward. Maybe I'll use Java after all...
Thanks for the heads up about the Data Generator Tool, but for now I'm on v9.9.4 per corporate policy : )
0 -
Very true that FESI is outdated. In the current version of the product, the default Javascript engine is Oracle Nashorn and the dead link has been removed. Another option if you want to continue to use JavaScript is to add the Nashorn engine to Virtualize - see the docs about how to add JSR 223 scripting languages to Virtualize.
1