# How to only display part of msg.payload?

**URL:** https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802
**Category:** Dashboard
**Created:** [30 October 2022 18:52 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802 "2022-10-30T18:52:23Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)
#### Post date: [30 October 2022 18:52 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/1 "2022-10-30T18:52:23Z")

</div>

I am receiving a message in node red via MQQT and only wish to display part of the payload of the message and am not sure how to do it.

A typical message is;  
{"topic":"sofar/status","payload":"State1 = DISCHARGE\nState2 = DISCHARGE\nState3 = DISCHARGE\n","qos":0,"retain":false,"\_msgid":"d014745cacc84574"}

I wish to just display what State1 equals

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [30 October 2022 19:14 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/2 "2022-10-30T19:14:54Z")

</div>

Set the MQTT in node to parse Json as object the read the following...

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

### Author: ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)
#### Post date: [30 October 2022 19:17 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/3 "2022-10-30T19:17:27Z")

</div>

Yes, thanks I'd read that, but the part I want is within the payload along with other data.

---

<div class="post-metadata">

### Author: ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)
#### Post date: [30 October 2022 19:59 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/4 "2022-10-30T19:59:24Z")

</div>

Sorted by using [node-red-contrib-string (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-string)

```auto
[{"id":"22e4ddb4fadf9914","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 2","methods":[{"name":"between","params":[{"type":"str","value":"\\n"},{"type":"str","value":"\\n"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":480,"wires":[["d09aa9c4fadd0e97"]]},{"id":"6c8914db442c25ea","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 1","methods":[{"name":"between","params":[{"type":"str","value":""},{"type":"str","value":"\\n"}]},{"name":"append","params":[{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":520,"wires":[["eefdac59326b8149"]]},{"id":"b2107428bb9504c4","type":"string","z":"2af48c1b36d78bb5","name":"Inverter 3","methods":[{"name":"delLeftMost","params":[{"type":"str","value":"State2"}]},{"name":"between","params":[{"type":"str","value":"\\n"},{"type":"str","value":"\\n"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":560,"wires":[["3556bcc46f45afa2"]]}]

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [31 October 2022 08:57 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/5 "2022-10-31T08:57:59Z")

</div>

Sorry, i missed the point you were trying to extract key-value pairs from the payload.

I would probably skip the contrib node and do it in a function and generate a proper object...

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

**FUNCTION: split out key-value pairs**

```auto
const data = msg.payload.split('\n')
const entries = []
data.forEach(function(element) {
    element = element.trim()
    if(!element) { return } // skip empty
    const parts = element.split(" = ")
    entries.push([parts[0].trim(), parts[1].trim()])
});
msg.payload = Object.fromEntries(entries)
return msg;

```

---

<div class="post-metadata">

### Author: ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)
#### Post date: [31 October 2022 15:56 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/6 "2022-10-31T15:56:22Z")

</div>

Thank you, that is very neat. (I'm still learning programming, used to do Basic and moved on to C and now continuing onwards ).

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [31 October 2022 19:57 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/7 "2022-10-31T19:57:22Z")

</div>

You will get a lot more out of Node-RED by learning JavaScript, but in the meanwhile, if you explore the capabilities of the core nodes you will find that lots of problems (like yours) can be solved without writing any code.

 ![Untitled](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73a7c314496ae7da2cc344ffd7a6855bce5d2733.jpeg)

```auto
[{"id":"fba3439d.857828","type":"function","z":"67af0149.01c99","name":"fake mqtt data","func":"msg.payload = \"State1 = DISCHARGE\\nState2 = DISCHARGE\\nState3 = DISCHARGE\"\nmsg.topic = \"sofar/status\"\nmsg.qos = 0\nmsg.retain = false\nmsg._msgid = \"d014745cacc84574\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":320,"wires":[["b2dd2e0e.1cf33"]]},{"id":"85183425.06efa","type":"inject","z":"67af0149.01c99","name":"start","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":320,"wires":[["fba3439d.857828"]]},{"id":"bc82f146.29b38","type":"debug","z":"67af0149.01c99","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":380,"wires":[]},{"id":"b2dd2e0e.1cf33","type":"split","z":"67af0149.01c99","name":"split on \\n","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":470,"y":320,"wires":[["aa8f86cd.a0486"]]},{"id":"aa8f86cd.a0486","type":"switch","z":"67af0149.01c99","name":"select State1","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"State1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":620,"y":320,"wires":[["87565481.398b3"]]},{"id":"87565481.398b3","type":"change","z":"67af0149.01c99","name":"extract value","rules":[{"t":"change","p":"payload","pt":"msg","from":"State1 = ","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":380,"wires":[["bc82f146.29b38"]]}]

```

---

<div class="post-metadata">

### Author: ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)
#### Post date: [31 October 2022 21:04 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/8 "2022-10-31T21:04:57Z")

</div>

Yes trying to learn Javascript. One thing with Node-RED is their are so many different ways of doing the same thing!! That's 3 in this thread now. 😀

---

<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: [14 November 2022 21:05 UTC](https://discourse.nodered.org/t/how-to-only-display-part-of-msg-payload/69802/9 "2022-11-14T21:05:29Z")

</div>

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