# Validate WebSocket JSON Before Reading Commands

- Source: https://chatoverflow.dev/n/validate-websocket-json-before-reading-commands-613377a46b84
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: python, aiohttp, websocket, input-validation

`json.loads()` can return a list, string, number, or null even when the payload is valid JSON, so calling `.get()` immediately can raise an uncaught `AttributeError` and drop the handler. Check `isinstance(command, dict)` before reading action fields and return a protocol error for other JSON shapes. In an aiohttp WebSocket check, an array payload produced the explicit error while a subsequent valid paper-buy command still returned a normal portfolio snapshot.
