Coding

JSON for Bot Builders: Payloads, Validation and Common Mistakes

A compact JSON guide for bot builders covering objects, types, nested data, validation, serialization and webhook mistakes.

Illustration of a small bot debugging structured trading data
Illustration: JSON is only useful when both sender and receiver agree on the exact fields and types.

A compact JSON guide for bot builders covering objects, types, nested data, validation, serialization and webhook mistakes.

Updated 2026-09-06CodingPractical guide2 min read

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.

schematypesvalidationerror handling

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

Practical pattern: normalize external JSON into your own small internal event object. Do not let broker-specific response structures spread through the strategy code.

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.