# How to create a filename using jsonata in file write node

**URL:** <https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407>\
**Category:** General\
**Created:** [10 March 2023 19:41 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407 "2023-03-10T19:41:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daled](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@daled](https://discourse.nodered.org/u/daled)\
**Post date:** [10 March 2023 19:41 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/1 "2023-03-10T19:41:04Z")

</div>

I'm relatively new to node-red. I've searched and cannot seem to find a "simple" solution to what I want to do. It seems like I should be able to use a jsonata expression to create a filename in my write file node. I want the filename to be of the format:  
/user/public/documents/battery/[msg.topic]\_[YYYY-MM-DD].txt  
where msg.topic is the message topic and YYYY-MM-DD is the current year, month and day.

I cannot seem to figure out to correct syntax to do this. I know I can do it with code in a function node and assign it to msg.filename, but I'd like to understand how to do it with jsonata. I "asked" ChatGPT how to do it and it came up with the following line, which does not work (for a number of reasons)

/users/public/documents/battery/[[msg.topic]]\_[[moment().format('YYYY-MM-DD')]].txt

Can this be easily done? If so, how?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 19:53 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/2 "2023-03-10T19:53:28Z")

</div>

> [@daled](#):
>
> /users/public/documents/battery/[[msg.topic]]\_[[moment().format('YYYY-MM-DD')]].txt

```auto
"/users/public/documents/battery/" & $$.topic & "_" & $moment().format('YYYY-MM-DD') & ".txt"

```

---

<div class="post-metadata">

**Author:** ![daled](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@daled](https://discourse.nodered.org/u/daled)\
**Post date:** [10 March 2023 20:05 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/3 "2023-03-10T20:05:07Z")

</div>

E1cid:  
Thanks for your very rapid response. I have node-red-contrib-moment installed, but your solution fails with the error on moment "attempted to invoke a non-function". Do I need to do something else to get moment() to work?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 20:06 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/4 "2023-03-10T20:06:48Z")

</div>

I copied your text but failed to notice there was no $ before moment  
edit post above.  
p.s. I would add hours mins and seconds, other wise you could end up with all files on same day overwriting each other, unless that is what you want.

---

<div class="post-metadata">

**Author:** ![daled](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@daled](https://discourse.nodered.org/u/daled)\
**Post date:** [10 March 2023 20:13 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/5 "2023-03-10T20:13:30Z")

</div>

I want to append to the file and have one file for each topic for each day. Thanks.

Is there someplace where the jsonata syntax/semantics for node-red is described simply?

Why $$.topic and not msg.topic in this context?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 20:17 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/6 "2023-03-10T20:17:01Z")

</div>

Google JSONata and one of the top one's will be [jsonata.org](http://jsonata.org) docs

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 March 2023 20:17 UTC](https://discourse.nodered.org/t/how-to-create-a-filename-using-jsonata-in-file-write-node/76407/7 "2023-03-24T20:17:20Z")

</div>

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