# How to calculate the length of msg.payload?

**URL:** <https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219>\
**Category:** General\
**Tags:** function-node\
**Created:** [14 December 2022 19:57 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219 "2022-12-14T19:57:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![json\_lame](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@json\_lame](https://discourse.nodered.org/u/json_lame)\
**Post date:** [14 December 2022 19:57 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/1 "2022-12-14T19:57:49Z")

</div>

Hi all

I'm struggling with calculate length msg.payload. Is there any way to do it? In addition I want to put 0 in front of msg.payload in case of length of msg.payload is lower than 5 characters. Msg.payload is string.

Thanks for help 🙂

---

<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:** [14 December 2022 20:08 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/2 "2022-12-14T20:08:04Z")

</div>

[Strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) and arrays have a length property.  
`msg.payload.length` will return the string length as an integer

You can also just pad the start of the string if less than x chars.

[str.padstart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)  
`msg.payload = msg.payload.padStart(5,"0")`

---

<div class="post-metadata">

**Author:** ![znetsixe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/znetsixe/32/72588_2.png) [@znetsixe](https://discourse.nodered.org/u/znetsixe)\
**Post date:** [14 December 2022 20:11 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/3 "2022-12-14T20:11:44Z")

</div>

```auto
var alt_var = msg.payload ;
let msglength = alt_var.length ;

If ( msglength < 5 ) {
msg.payload = "0" + alt_var ;
}

return msg;

```

This would be a way to do it but best to read up in javascript functions?

---

<div class="post-metadata">

**Author:** ![json\_lame](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@json\_lame](https://discourse.nodered.org/u/json_lame)\
**Post date:** [14 December 2022 20:41 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/4 "2022-12-14T20:41:04Z")

</div>

@E1cid  
@znetsixe

Many thanks to you! 🙂 both works  
When I tried myself to use msg.payload.length I still received original value msg.payload...

---

<div class="post-metadata">

**Author:** ![Elberto](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Elberto](https://discourse.nodered.org/u/Elberto)\
**Post date:** [13 February 2023 16:27 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/5 "2023-02-13T16:27:46Z")

</div>

Hi to all!  
I have this debug as database output:

```auto
13/02/2023, 16:21:43[node: debug 22](http://127.0.0.1:1880/#)
SELECT name, lat, lon, type, status FROM stations : msg.payload : Object
object
name: "SIOT"
lat: 43.1339
lon: 11.06825
type: 0
status: 0

```

```auto
13/02/2023, 16:23:57node: debug 22
SELECT name, status FROM stations WHERE name = $stazione : msg.payload : Object
object
name: "BURA"
status: 0

```

Using msglength = alt\_var.length as new payload I always get name: undefined as output.  
I need to understand how discriminate between two database output queries....  
Thanks,

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 February 2023 16:48 UTC](https://discourse.nodered.org/t/how-to-calculate-the-length-of-msg-payload/72219/6 "2023-02-13T16:48:15Z")

</div>

Your debug suggests that msg.payload is an object.

Try `var size = Object.keys(msg.payload).length;`  
Object.keys() gives you an array `["name", "status"]` An array has a length property.
