# Split of string using JSONata in a change node?

**URL:** <https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778>\
**Category:** General\
**Created:** [26 February 2023 23:20 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778 "2023-02-26T23:20:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [26 February 2023 23:20 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/1 "2023-02-26T23:20:20Z")

</div>

OK, so I like many of us that us NR, see JSONata as this mystical experience that works at times well other times just not quite right way to do something. I just going to write a small function node to extract a string from a MQTT subscription string... and thought, hey, can I do this in a change node expression? Maybe even with JSONata?

Where msg.topic is 'command/target/status', and I want to pull 'target' from the topic? In a function node, easy, like msg.target = msg.topic.split(/)[1]. But a function node for a one-liner? How could you implement this in a change node? Of course the following does not work...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e319916063c6432aeecbf5634f0ef256ad3ec045.png)

But hey, someone that know change nodes, expressions, or JSONata better than me, know a trick? Or am I just too far outside the box on this on?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 February 2023 23:29 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/2 "2023-02-26T23:29:46Z")

</div>

```auto
msg.topic.split(/)[1]

```

is not what you specified in the expression.  
but split is a function within jsonata, every function starts with `$`:

```auto
topic.$split("/")[1]

```

(omit `msg`)

---

<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:** [26 February 2023 23:54 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/3 "2023-02-26T23:54:22Z")

</div>

That would be

```auto
$split($$.topic, "/")[1]

//or

($$.topic ~> $split("/"))[1]

```

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [27 February 2023 00:00 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/4 "2023-02-27T00:00:45Z")

</div>

OK, I get that the functions are referenced by $, but the $$ is giving me a head scratching. It reminds me a bit of a powershell or bash reference, like $? (bash) or $\_ (powershell). How would one know or learn of such as $$ use and syntax? Clearly $.topic is a type of substitution? Shorthand reference?

---

<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:** [27 February 2023 00:03 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/5 "2023-02-27T00:03:31Z")

</div>

it's just my convention, you can omit them if you wish. It comes into play with more complicated expressions. It reminds me what scope the property is in.  
$$ is the msg/base scope, while $ is the curent scope.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [27 February 2023 00:24 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/6 "2023-02-27T00:24:56Z")

</div>

Yeah, now I get it... like bash $? or powershell $\_ is the 'current' context scope.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 February 2023 06:21 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/7 "2023-02-27T06:21:04Z")

</div>

> [@E1cid](#):
>
> That would be

Well, "it works", but it is inherently slow, would not recommend it on large objects.

Just traverse down the path and start from there.  
and this works perfectly fine `topic.$split("/")[1]`

---

<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:** [27 February 2023 07:22 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/8 "2023-02-27T07:22:58Z")

</div>

> [@bakman2](#):
>
> Well, "it works", but it is inherently slow, would not recommend it on large objects.
> 
> Just traverse down the path and start from there.  
> and this works perfectly fine `topic.$split("/")[1]`

Well when i tested there was no difference, running 1000 operations the average for both was 160ms. Again I do it as a matter of consistency and with more complicated expressions it makes sure i do not call a property from the wrong scope, since doing so I find less expressions fail. It works for me and cost me nothing.

`topic.$split("/")[1]` tells JSONata to map over topic as if an array and split each element and output index 1.  
`$split($$.topic, "/")[1]` tells JSOnata to split topic and output index 1.  
Both work just fine, as JSONata treats single string as a 1 element array.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 February 2023 08:11 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/9 "2023-02-27T08:11:01Z")

</div>

It is [no secret](https://www.nearform.com/blog/the-jsonata-performance-dilemma/) that JSONata can be 100's of times slower than native javascript.

Ofcourse, this does not become a dealbreaker with small(er) objects like this example, but if the path is known I would choose to traverse from there.

---

<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:** [27 February 2023 15:43 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/10 "2023-02-27T15:43:12Z")

</div>

It is normally not 100's of times slower, bit of an exaggeration to say that. It is slower 1 to 4 ti.es normally, unless you have written a bad expression

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [22 March 2023 15:00 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/11 "2023-03-22T15:00:05Z")

</div>

So, the bottom line is... abusing a function node, if you get my meaning? Is really leaner and meaner, even though it does hide some of the elegance of a visual language. Just an observation.

---

<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:** [21 May 2023 15:00 UTC](https://discourse.nodered.org/t/split-of-string-using-jsonata-in-a-change-node/75778/12 "2023-05-21T15:00:57Z")

</div>

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