JSON-RPC における id について
Published: 2023/11/5
JSON-RPC は、呼び出す際のトップレベルのフィールドに、 "id"
を指定する。
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
この "id"
は何を表すのか、について。
答え: 任意のリクエストID
JSON RPC - What is the "id" for?
I don't understand what the ID is for in JSON RPC. Also, how bad is it considered to not use JSON-RPC.org's standards when developing a toolkit? There seems to be some ambiguity in the JSON-RPC wor...
stackoverflow.com

You're not guaranteed to get your answers back in the order you asked for them; the id is to help you sort that out.
とある通り、 JSON-RPC は例えば websocket 上で実行される場合などでは、同一コネクションで複数のプロシージャを非同期実行、などが行えたりする。
その際、どのレスポンスがどのリクエストに対応していたかを呼び出し側は知る必要があるため、 JSON-RPC のレスポンスに、リクエストの際に指定されていた "id"
と同じ値が返ってくるようになっている。
JSON-RPC 公式の例で言えば、
--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
<-- {"jsonrpc": "2.0", "result": -19, "id": 2}
<-- {"jsonrpc": "2.0", "result": 19, "id": 1}
なども(実装によっては)ありうるため、である。