A compact JSON guide for bot builders covering objects, types, nested data, validation, serialization and webhook mistakes.
In plain English
This guide focuses on making data and code predictable: define types, validate inputs, handle errors explicitly and test the cases that should fail.
JSON is a data format, not executable code
JSON is useful because nearly every webhook and REST API can send or receive it. It supports objects, arrays, strings, numbers, booleans and null. It does not support comments, Python tuples, JavaScript functions or trailing commas.
{
"event_id": "abc-123",
"symbol": "BTCUSD",
"action": "buy",
"size": 0.01,
"meta": {"source": "tradingview", "version": 1},
"tags": ["breakout", "demo"],
"dry_run": true
}
Strings versus numbers
"0.01" is a string; 0.01 is a number. Many integrations fail because the producer and receiver disagree about types. Decide a schema and enforce it before the event can reach execution code.
def validate_event(d):
if not isinstance(d.get("event_id"), str):
return False, "event_id_not_string"
if d.get("action") not in {"buy", "sell", "close"}:
return False, "bad_action"
if not isinstance(d.get("size"), (int, float)):
return False, "bad_size"
return True, "ok"
Serialization
In Python, json.dumps() turns Python data into JSON text and json.loads() parses JSON text. The requests library can usually send a JSON body using the json=payload argument, which also sets an appropriate content type.
Common mistakes
- Using single quotes around JSON keys.
- Leaving a trailing comma after the last field.
- Assuming missing and
nullmean the same thing. - Accepting arbitrary extra fields without deciding whether they are safe.
- Putting passwords or API keys inside a webhook payload.
- Logging the full payload when it contains secrets.
Before you rely on this in production
- Input types are validated.
- Errors are handled instead of swallowed.
- Tests include invalid and boundary cases.
- Secrets are not embedded in code or test fixtures.
