Search code examples
jsonhttpprotocolsmime-types

How is JSON data transmitted byte wise via HTTP


Since HTTP is a text protocol I assume that for all mime types the HTTP body typically consists of text. This would mean that for JSON all numbers would be represented as text with 1 byte for each character instead of 8 bytes flat.

E.g. for transmitting this JSON:

{ num: 0.123456789 }

It would transmit 11 bytes alone for the number value.

Is this correct or are there optimized byte representations for different mime types and in particular JSON in HTTP?


Solution

  • HTTP can send binary data just fine, and there's 2 ways in particular to optimize this:

    1. You can switch to a binary encoding that's not JSON but largely compatible with JSON. CBOR is one example.
    2. You can gzip or brotli-compress the JSON. Browsers support this transparently.

    Option 2 is by far the easiest and actually gives you a great bang for your buck. But option 1 usually wins in terms of efficiency of sending bytes and can be combined with 2.