35

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 world.

P.S. The ID I'm referring to is the id in here:

{"params":["Hello","World"],"method":"hello_world","id":1}
orokusaki
  • 55,146
  • 59
  • 179
  • 257

5 Answers5

40

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.

Andrew McGregor
  • 31,730
  • 2
  • 29
  • 28
  • 3
    Oh, in the case of asynchronous calls. That makes sense. – orokusaki Feb 05 '10 at 22:29
  • 10
    What if JSON-RPC over HTTP? ID seems to be useless since HTTP is designed by request-response pattern. – Weihang Jian Jul 19 '14 at 04:45
  • 6
    @JianWeihang The specification is transport-agnostic. So yes, it will not have much use on a HTTP request that only have one JSON-RPC request. But if it is a multi-call JSON-RPC request, in a single HTTP request, the IDs will be used to map the responses correctly, since they will also come all in a single HTTP response. – tstark81 Sep 25 '15 at 03:55
13

The "id" is returned in the corresponding response object, so you can map one context to the other.

If you are making synchronous single calls, it might not make sense, but in an async multi-outstanding-call enviroment it is vital.

It should not be hard coded to 1, but set to a unique value for every request object you generate from the client.

MPCM
  • 251
  • 2
  • 4
9

None of the answers mentions the difference between the two existing versions of the protocol.

JSON RPC 1.0:

The request id. This can be of any type. It is used to match the response with the request that it is replying to.

JSON RPC 2.0:

An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts.

Thus it is perfectly fine in JSON RPC 2.0 to set id to some fixed value. But be aware of the use of id in batch requests.

ben
  • 5,671
  • 4
  • 27
  • 55
2
  1. To indicate you're expecting a response. (Without it, there should be no response.)
  2. To match responses to requests when using asynchronous or batch calls.
bcb
  • 1,977
  • 2
  • 22
  • 21
-2

You can read the JSON RPC docment https://www.jsonrpc.org/specification. In the "4 Request object" the id param is explained:

id

An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain fractional parts [2] The Server MUST reply with the same value in the Response object if included. This member is used to correlate the context between the two objects.

[1] The use of Null as a value for the id member in a Request object is discouraged, because this specification uses a value of Null for Responses with an unknown id. Also, because JSON-RPC 1.0 uses an id value of Null for Notifications this could cause confusion in handling.

[2] Fractional parts may be problematic, since many decimal fractions cannot be represented exactly as binary fractions.

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
TinyXu
  • 11