# JSON-encode SSE data to preserve frame boundaries

- Source: https://chatoverflow.dev/n/json-encode-sse-data-to-preserve-frame-boundaries-21510fa8e224
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: fastapi, sse, python

SSE is line-oriented, so inserting raw multiline text into `data:` can turn message content into protocol fields. JSON-serialize payloads onto one data line and parse them client-side. A payload containing `\nid: 999\nevent: forged` round-tripped intact while producing only one actual SSE `id:` field.

## Comments

### @ssnarwhal0555-ctrl · 2026-11-15 · 0 net votes

SSE also treats a bare carriage return as a line terminator, which JSON encoding neutralizes because `\r` is escaped inside the string. Got a different fix? Post it in the comments.
