# How to find the size of mqtt data in python

**URL:** <https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862>\
**Category:** General\
**Tags:** mqtt, python\
**Created:** [1 June 2023 09:43 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862 "2023-06-01T09:43:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnson](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@Johnson](https://discourse.nodered.org/u/Johnson)\
**Post date:** [1 June 2023 09:43 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/1 "2023-06-01T09:43:09Z")

</div>

Hi, I need to find the packet size of a mqtt data in python.

My question is, how can i find the mqtt packet size which include the control header, packet length, variable length header and payload .

At the moment i am using **getsizeof** to find it.But i am not sure whether it gives the payload alone or the complete size.

My data is in the form : **b'{"0":294.3875741324,"1":292.1924140501}'**  
Size i get for this data with getsizeof is **72**.

I hope somebody can help me.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 June 2023 12:05 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/2 "2023-06-01T12:05:55Z")

</div>

I'd ask this on a python forum or maybe the MQTT Google group [https://groups.google.com/g/mqtt](https://groups.google.com/g/mqtt)

---

<div class="post-metadata">

**Author:** ![Johnson](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@Johnson](https://discourse.nodered.org/u/Johnson)\
**Post date:** [1 June 2023 12:16 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/3 "2023-06-01T12:16:15Z")

</div>

Ok .Thank you for the link.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [1 June 2023 15:33 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/4 "2023-06-01T15:33:13Z")

</div>

> [@Johnson](#):
>
> **b'{"0":294.3875741324,"1":292.1924140501}'**

Try this Python snippet...

```auto
data = b'{"0":294.3875741324,"1":292.1924140501}'
string_data = data.decode('utf-8') # Convert bytes to string using UTF-8 encoding
size = len(string_data)
print(size) # Output: 39

```

---

<div class="post-metadata">

**Author:** ![Johnson](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@Johnson](https://discourse.nodered.org/u/Johnson)\
**Post date:** [5 June 2023 11:47 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/5 "2023-06-05T11:47:49Z")

</div>

Thank you for your reply .  
Is it possible to use the same method for finding the size of the complete message .

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 June 2023 18:05 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/6 "2023-06-05T18:05:32Z")

</div>

If you post one of your typical, complete MQTT messages, I'll take a look.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 June 2023 19:00 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/7 "2023-06-05T19:00:36Z")

</div>

Here's a simple example using part of a MQTT message from one of my weather stations.

```auto
readings = {
    "RCS_version": "v74D.py",
    "sensors": {"temp":21.6, "humidity":90, "pressure":1002},
    "hostname": "monday_test"
}

size = len(str(readings))
print("Size of the string representation of the dictionary: ", size)

```

```auto
Size of the string representation of the dictionary: 114

```

---

<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:** [4 August 2023 19:01 UTC](https://discourse.nodered.org/t/how-to-find-the-size-of-mqtt-data-in-python/78862/8 "2023-08-04T19:01:15Z")

</div>

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