JSONata: taking substring of message attr

I apparently haven't been paying attention when it comes to JSONata, even for simple use cases. I have a simple situation where msg.myrawdate contains a string like "2020-06-12T12:48:24.559Z". I want to extract the first 11 characters of that to msg.payload so I have configured a Change node to Set msg.payload to JSONata $substring(myrawdate, 0, 11). However this gives an error
"Invalid JSONata expression: Argument 1 of function "substring" does not match function signature".
I presume I am missing something simple, but don't know what. I have looked in vain for similar examples, I think possibly it is too simple to be considered worthy of an example.

So you are trying to use JSONata on a string???

Just on the eye it looks to be fine.
https://try.jsonata.org/Dh8lwT45D
Though are you sure you want to include the T for the timestamp too?
Can you post a sample message going into the change node for checking? The error you got suggests that msg.myrawdate is not a string.
@Colin is it possible that there are messages going into that change node that don't have a myrawdate attribute on the message object? It's likely going to go wrong on that.

Aargh, I was confused by the information tab for node-red-contrib-simpletime. It shows, for msg.myrawdate that typical value is

msg.myrawdate: 
"2018-09-11T18:51:17.064Z"

but of course actually it means that myrawdate contains a Date object, not the string. There is nothing wrong with the JSONata at all. I should have realised that much earlier.
Thanks all.
(and yes, I do want the "T", I am comparing filenames that use an iso date string in the name and I want to check the day part but ignore the time).

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.