# Extract labels & values from MQTT message

**URL:** <https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339>\
**Category:** General\
**Created:** [27 February 2020 19:14 UTC](https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339 "2020-02-27T19:14:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 February 2020 19:14 UTC](https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339/1 "2020-02-27T19:14:13Z")

</div>

On a cloud node-RED instance I receive a MQTT message of power data (in the influx line protocol format);  
`grafana,device=diverter,type=energy grid=264,solar=0,divert=0,volts=242.71,accusage=0.7333333333333333,accsolar=0,accdivert=0,temp=10.87,usage=264,rssi=-68 1582828964661000000`  
and I would like to parse the data pairs into an object payload.

The string is split into 3 sections, with each section being defined by a space;  
The first section (which I don't need) - `grafana,device=diverter,type=energy`  
The second section (needed, but in a format that I can use) - `grid=264,solar=0,divert=0,volts=242.71,accusage=0.7333333333333333,accsolar=0,accdivert=0,temp=10.87,usage=264,rssi=-68`  
and the final section ( a timestamp which I also need) - `1582828964661000000`

So far, I have a function which breaks up the 3 sections;

```auto
const x = msg.payload.trim().split(" ");
const data = x[1];
const timestamp = x[2];

```

and which results in `data` =  
`grid=264,solar=0,divert=0,volts=242.71,accusage=0.7333333333333333,accsolar=0,accdivert=0,temp=10.87,usage=264,rssi=-68`  
and in `timestamp` = the timestamp `1582828964661000000` (I can sort the timestamp ok)

But how can I further progress the `data`?

Advice would be appreciated.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [27 February 2020 19:35 UTC](https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339/2 "2020-02-27T19:35:30Z")

</div>

```auto
let arr = msg.payload.split(" ")
arr.shift()
let pairs = arr[0].split(",")
let result = {}
function addToResult(ob){
    let a = ob.split("=")
    result[a[0]] = a[1] 
}
pairs.map(ob => addToResult(ob))
result.timestamp = parseInt(arr[1])/1000

msg.payload = result
return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 February 2020 19:40 UTC](https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339/3 "2020-02-27T19:40:16Z")

</div>

Wow!!  
Many thanks, perfect 👍

---

<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:** [12 March 2020 19:48 UTC](https://discourse.nodered.org/t/extract-labels-values-from-mqtt-message/22339/4 "2020-03-12T19:48:14Z")

</div>

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