JSON-RPC 2.0: Correlate Request IDs, Notifications, and Batch Responses
JSON-RPC 2.0 represents remote method calls as JSON objects. Match responses to requests by `id`; a notification omits `id` and must not receive a response. The specification defines message shapes, not transport, authentication, or retry policy, so check the service's separate contract.
JSON-RPC 2.0 represents remote method calls as JSON objects. Match responses to requests by id; a notification omits id and must not receive a response. The specification defines message shapes, not transport, authentication, or retry policy, so check the service's separate contract.
Build a request object
A request contains jsonrpc: "2.0", a method name, optional params, and an id for correlating the response. Parameters may be an object with named values or an array of positional values. Array position matters; object member names identify the parameters.
{"jsonrpc":"2.0","method":"lookupUser","params":{"userId":42},"id":"req-17"}An ID may be a string, number, or null. In practice, use a distinct string or number for each outstanding request so responses can be matched reliably. Omitting id makes the message a notification. Although null is a valid ID, it can make an error response difficult to correlate, so avoid it when possible.
Distinguish success from errors
A success response contains the request id, jsonrpc: "2.0", and result. An error response has an error object instead of result; it contains an integer code and a human-readable message, and may include data. A response must contain either result or error, not both.
Standard codes include parse error -32700, invalid request -32600, method not found -32601, invalid params -32602, and internal error -32603. Implementations may use -32000 through -32099 for server errors. Handle unknown codes, and do not treat the message text as a stable machine-readable signal.
When to use a notification
A notification is a request without id. The server must not send a JSON-RPC response, even if it processes the message. The caller therefore cannot confirm its result or error. Use notifications only when no response is needed. id: null is different: it is a request that may receive a response; omitting id creates a notification.
Handle batch responses
A batch puts multiple request objects in one JSON array. The server may return an array of responses for non-notification items; notifications have no response. Response order is not guaranteed to match request order, and a server may process items concurrently or in any order. Match each response by its id, never by array position. Do not assume a batch is atomic or that every item always appears in one response array; check the implementation's contract.
[
{"jsonrpc":"2.0","method":"lookupUser","params":{"userId":42},"id":"a"},
{"jsonrpc":"2.0","method":"recordView","params":{"page":"home"}}
]The first item expects a response; the second is a notification. Validate the JSON and response shape, map each ID to a pending request, then handle result and error separately.
Define transport behavior separately
JSON-RPC is transport-agnostic. It can be carried over HTTP, WebSocket, standard input/output, or another transport, but the message specification does not determine HTTP status codes, content types, authentication, size limits, deadlines, retries, or cancellation behavior. Check the API provider's transport rules.
If a connection drops or a deadline expires, the client may not know whether the server ran the method. Automatically retrying a side-effecting request can duplicate work. A unique ID correlates a response; it is not an idempotency key or a deduplication mechanism.
Implementation checklist
Verify `jsonrpc` is `"2.0"` and method parameters match the contract.
Use a unique ID per request and correlate responses by ID.
Do not wait for a notification response; include an ID when a result is needed.
Validate that `result` and `error` are mutually exclusive and handle codes.
Do not assume batch items run in order or atomically.
Check transport authentication, status codes, limits, deadlines, and retry rules separately.
Source
[JSON-RPC 2.0 Specification](https://www.jsonrpc.org/specification)