sys.getsizeof()
will give you the size of an object in memory. But from your description, it sounds like you're looking for the length of some serialization (into a message) of the dictionary.
It looks like you're using JSON, and that makes sense. For example using json.dumps()
:
json_string = json.dumps(your_dict)
The next question is how do you get the length (in octets) of that string.
Well len(json_string)
will give you the number of characters, but for most encodings, the number of bytes required to transmit those characters will be different.(Docs)
So first you need to encode your string to bytes, then use the length of the resulting bytes object:
len(json_string.encode(<your encoding>))
Which will give you the number of octets needed to transmit that dictionary.
Note: any other requirements of the message, such as headers, delimiters, escaping, formatting, etc will be in addition to this number.