When building custom features in Odoo, communication between the frontend (JavaScript) and backend (Python) often becomes a key requirement. JSON-RPC controllers provide a quicker, more organized, and cleaner method of sending and receiving data than Odoo's conventional web controllers, particularly for dynamic user interface elements like dashboards, custom widgets, or external integrations.
This blog will explain what JSON-RPC is, why Odoo uses it, and provide a real-world example of how to build your own JSON-RPC controller.
What is JSON-RPC?
A lightweight remote procedure call (RPC) protocol called JSON-RPC allows:
- To invoke a method, the client submits a JSON request.
- The outcome is contained in a JSON response that the server provides.
- Communication is easy to debug, fast, and language-neutral.
JSON-RPC is widely used internally in Odoo; form views, list views, and numerous JS components depend on it to communicate with the server.
Why JSON-RPC in Odoo 19?
A more distinct division between various controller types is introduced starting with Odoo 19. Instead of using type="json" as in earlier versions, Odoo 19 encourages developers to use type="jsonrpc".This indicates that the route is intended for requests and responses in the JSON-RPC style.
Odoo 19's JSON-RPC controllers work best when:
- You want frontend JavaScript and backend Python to communicate in an organized manner.
- In the WebClient, you are creating dynamic elements (OWL widgets, dashboards, kanban metrics, etc.).
- You require a quick and lightweight API endpoint.
- You desire a consistent format for requests and responses.
For internal operations, such as loading views, updating fields, retrieving data for widgets, and managing interactive UI updates, Odoo itself mainly depends on JSON-RPC.
Creating a JSON-RPC Controller in Odoo 19
In Odoo 19, the @http.route decorator with type="jsonrpc" is used to define a JSON-RPC endpoint or controller.
This guarantees that Odoo will only accept and return data in the JSON-RPC format.
For Example:
@http.route('/demo/jsonrpc/greet', type='jsonrpc', auth='public')
def greet_user(self, name=False):
message = f"Hello, {name}!" if name else "Hello from Odoo 19 JSON-RPC!"
return {
"status": "success",
"greeting": message,
}What this method does
- Exposes a JSON-RPC endpoint: /demo/jsonrpc/greet
- Accepts parameters passed from the frontend (name)
- Returns a JSON-formatted response
- Works even for unauthenticated users (auth='public')
How JSON-RPC Requests and Responses Look
Example Request Payload
{
"jsonrpc": "2.0",
"method": "call",
"params": { "name": "Odoo Developer" },
"id": null
}Example Response Payload
{
"jsonrpc": "2.0",
"result": {
"status": "success",
"greeting": "Hello, Odoo Developer!"
},
"id": null
}Use Cases and Best Practices
When you require quick, organized communication between the frontend and backend, JSON-RPC controllers are particularly helpful. They are frequently used in Odoo 19 to create interactive kanban enhancements, dynamic dashboards, real-time metric cards, and custom OWL components that retrieve or update data without requiring a page reload. Internal tools or micro-applications developed within the Odoo WebClient, where frequent and minimal data requests are needed, also function well with them.
Use best practices when implementing JSON-RPC, such as using auth="user" for routes that expose sensitive business information, validating all incoming parameters, and returning only JSON-serializable data. To enhance performance, minimize your responses and refrain from carrying out complex calculations directly within controller methods. JSON-RPC becomes a clean and effective way to create contemporary, interactive features in Odoo 19 when your endpoints are properly secured and structured.
Conclusion
Odoo 19's JSON-RPC controllers provide a strong, effective, and user-friendly method of connecting the frontend and backend. JSON-RPC offers a clear and consistent communication layer that enhances performance and maintainability, whether you're creating dynamic dashboards, sophisticated widgets, or interactive tools within the WebClient. Odoo 19's move to type="jsonrpc" also encourages clearer architecture, which will better align your custom modules with Odoo's internal communication standards. Developers can create modular components that scale better across various modules, reduce boilerplate code, and achieve more responsive user interface behavior by utilizing JSON-RPC.
JSON-RPC becomes a crucial tool for creating cutting-edge, real-time experiences inside Odoo if it is implemented with appropriate validation, security, and optimized responses. JSON-RPC will only become more essential to custom development as Odoo moves toward a more frontend-driven ecosystem using OWL.
To read more about How to call JSON RPC to Webcontroller in Odoo 19, refer to our blog, How to call JSON RPC to Webcontroller in Odoo 19.