Store unstructured JSON safely with json.RawMessage

6854
0

Not every integration payload maps cleanly to a static struct. When I need to accept “mostly known” JSON but preserve unknown fields for auditing or forward compatibility, I use json.RawMessage. That allows me to decode the envelope (event name, version, timestamps) while storing the original payload bytes for later processing. The key is to validate what you depend on (like type and id) and treat everything else as opaque. In production, this is useful for webhook ingestion pipelines: you can store the raw payload, then parse it asynchronously with versioned logic, without breaking ingestion when the upstream adds fields. It also improves debuggability because you can reprocess historical events from storage. Just be careful to size-limit payloads and avoid logging raw bodies.