MQTT Sparkplug - Optimizing bandwith at JSON "transfer"

Hi,
Anyone knows what is the most efficient way in terms of communication bandwith, to transfer a complete JSON object from point A to B using Sparkplug?

For now I cast a JSON object as a string, and use sparkplug type “String” as the basic type. Is it more efficient to “transform” the object into bytes, and then use sparkplug type “Bytes”?

Here is one example of a complete JSON object:

{
    "topic":"sensor_data",
    "payload":{
        "nodeId":0,
        "firmware":3,
        "battery":3.2940600000000004,
        "counter":37,
        "sensor_type":1,
        "sensor_data":{
            "humidity":45.51,
            "temperature":23.91
        },
        "type":"sensor_data",
        "addr":"00:13:a2:00:41:07:18:81",
        "original":{
            "mac":"00:13:a2:00:41:07:18:81",
            "receive_options":{
                "ack":0,
                "broadcast":0,
                "type":""
            }
        },
        "data": [127,0,3,3,255,37,0,1,0,17,199,9,87],
        "type":"receive_packet"
    },
    "_msgid":"391caba.5a19454"
}

Thanks.

JSON stands for “JavaScript Object Notation”. So it’s a notation or codification of an object, which means it will always be a string. Transferring it as bytes will just encode the string as bytes, and keep the same size.

If the object structure is fixed, you can create your own binary encoding for it (similar to how structs are saved in C), and reduce the bandwidth needed. But you’ll lose the versatility of JSON where it’s easy to add a certain key to an object.

If your JSONs are big, it could be worth to compress the JSON before sending it. That way you reduce bandwidth but it will cost more CPU time.

However, if the given example is an actual JSON it would send, at 700 bytes (or almost 400 if you remove the whitespace), you’re still well below the maximum of an ethernet MTU (traditionally 1500 bytes, minus the overhead). So it would still come in one package, and reducing the size won’t have a big effect.

1 Like

Great info!

I guess I will atleast remove any whitespaces.

I will also try to find some simple code of how to compress a JSON using javascript, to see how much space that can be saved.

I think i understand your comments regarding MTU. Maybe some investigation with wireshark on the MQTT packages will display fixed overhead sizes when using my specific Sparkplug setup. Thus opening up for a conditional compression based on JSON size.