# JSONata: Tip of the Day

**URL:** <https://discourse.nodered.org/t/jsonata-tip-of-the-day/24100>\
**Category:** General\
**Created:** [2 April 2020 15:12 UTC](https://discourse.nodered.org/t/jsonata-tip-of-the-day/24100 "2020-04-02T15:12:44Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [2 April 2020 15:12 UTC](https://discourse.nodered.org/t/jsonata-tip-of-the-day/24100/1 "2020-04-02T15:12:44Z")

</div>

T.O.T.D. 01 - splitting a string and picking out a part

Background: Let's say you have devices sending data into NR with topics like:

```auto
wemos/node41/solar/voltage
wemos/node13/sysinfo/upTime
wemos/node28/battery/voltage

```

and msg.payload contains a value.

You want to sent the data to an Iinflux DB and you want to send the fourth part of the topic as the ‘measurement’ (along with the value) and add the second and third parta as a tags. So how do you split them out?

By using the magic of JSONata!

In a `change` node, you can use the JSONata [`$split()` function](http://docs.jsonata.org/string-functions#split) to split a string into an array of substrings! In this case you would split the string using the slash (/), so using the expression  
`$split(topic,"/")` you would get back an array of

```auto
[
    "wemos",
    "node41",
    "bme280",
    "Humidity"
]

```

But how do you get the second item? Easy just add `[1]` at the end of the expression  
`$split(topic,"/")[1]`  
and it returns the second item in the array which is "node41"

So you can set  
msg.node to `$split(topic,"/")[1]` and it will be `node41`  
msg.device to `$split(topic,"/")[2]` and it will be `solar`  
msg.measurement to `$split(topic,"/")[3]` and it will be `voltage'

and your ready to build your database expression.

---

<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:** [1 June 2020 15:12 UTC](https://discourse.nodered.org/t/jsonata-tip-of-the-day/24100/2 "2020-06-01T15:12:46Z")

</div>

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