# JSONata: taking substring of message attr

**URL:** <https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227>\
**Category:** General\
**Created:** [12 June 2020 13:18 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227 "2020-06-12T13:18:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 June 2020 13:18 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227/1 "2020-06-12T13:18:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 June 2020 13:28 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227/2 "2020-06-12T13:28:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [12 June 2020 13:33 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227/3 "2020-06-12T13:33:40Z")

</div>

Just on the eye it looks to be fine.  
[https://try.jsonata.org/Dh8lwT45D](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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 June 2020 13:50 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227/4 "2020-06-12T13:50:45Z")

</div>

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

```auto
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).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 August 2020 13:50 UTC](https://discourse.nodered.org/t/jsonata-taking-substring-of-message-attr/28227/5 "2020-08-11T13:50:50Z")

</div>

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