Depth: 1/1

JSON-RPC

JSON-RPC is a widely adopted standard that describes structured messages for communication between a client and server. The current version is 2.0 (published in 2010).

It combines two technologies:

Note that “client” and “server” are used here to describe the two ends of the communication. In practice, the client and server could be any two programs that communicate with each other. Client and server roles could be reversed at different times in their lifecycle.

Use cases

JSON-RPC is used in many different contexts, including:

Message types

Request message

Request messages ask for an action (method) to be performed. The server must reply to a request with a response message.

A result and an error are mutually exclusive. If a response includes a result, it must not include an error, and vice versa.

A result can be any JSON value, including null. An error is an object with a code and a message property. An error can also include a data property with additional information about the error.

Response message

Response messages are the reply to a request. They include the id of the request they are replying to and a result or an error.

Notification message

Notification messages inform the other end about something that happened, and do not expect a reply.

Examples

Request & Response

Here, a client asks the server to add two numbers together. The server replies with the result.

Request:

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": [12, 5],
  "id": 1
}

Response:

{
  "jsonrpc": "2.0",
  "result": 17,
  "id": 1
}

Here, a client asks the server to add a number to a string. The server replies with an error.

Request:

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": [3, "cat"],
  "id": 2
}

Response:

{
  "jsonrpc": "2.0",
  "error": {
    "code": -32602,
    "message": "Invalid params",
    "data": "Cannot add a number to a string"
  },
  "id": 2
}

Notification

Here, the server informs the client that a user has logged in. The server doesn’t reply to this message but may use this notification to (e.g.) update the lastLoggedIn field in the user’s record or trigger a workflow.

{
  "jsonrpc": "2.0",
  "method": "userLoggedIn",
  "params": {
    "userId": 123
  }
}

Batch requests

JSON-RPC also supports batch requests, where a client sends multiple requests in a single message, and the server replies with multiple responses.

Request:

[
  {
    "jsonrpc": "2.0",
    "method": "add",
    "params": [12, 5],
    "id": 1
  },
  {
    "jsonrpc": "2.0",
    "method": "add",
    "params": [3, "cat"],
    "id": 2
  }
]

Response:

[
  {
    "jsonrpc": "2.0",
    "result": 17,
    "id": 1
  },
  {
    "jsonrpc": "2.0",
    "error": {
      "code": -32602,
      "message": "Invalid params",
      "data": "Cannot add a number to a string"
    },
    "id": 2
  }
]

Further reading

JSON-RPC

JSON-RPC is a widely adopted standard that describes structured messages for communication between a client and server. The current version is 2.0.

It combines two technologies:

Message types

Request message

Request messages ask for an action (method) to be performed. The server must reply to a request with a response message.

Response message

Response messages are the reply to a request. They include the id of the request they are replying to and a result or an error.

A result and an error are mutually exclusive. If a response includes a result, it must not include an error, and vice versa.

A result can be any JSON value, including null. An error is an object with a code and a message property. An error can also include a data property with additional information about the error.

Notification message

Notification messages inform the other end about something that happened, and do not expect a reply.

Further reading

JSON-RPC

JSON-RPC is a standard for communication between a client and server.

Official spec: https://www.jsonrpc.org/specification

Message types

JSON-RPC

JSON-RPC is a standard for communication between a client and server.

Official spec: https://www.jsonrpc.org/specification

JSON-RPC

JSON-RPC is a standard for communication between a client and server.

Official spec: https://www.jsonrpc.org/specification