Search code examples
jsonxml-rpcodooerp

Interaction with Odoo via XML-RPC


During my internship i'm told to build a Django application that retrieve data from Odoo via XML-RPC, and from my knowledge the xmlrpc web server support only XML format but when i follow the official documentation from Odoo10 https://www.odoo.com/documentation/10.0/api_integration.html in the "Search and read" section it seems to me like the response is in JSON format ( which is good for me). I'm just wondering if i'm missing something.

Any clarification ? thanks.


Solution

  • Not exactly JSON, but a Python object (list, dictionary, etc...) ready to be converted to a JSON string through the dumps method of the json library. Take a look at the behaviour of this Python console working with XMLRPC. First, I made the query (with also search_read method). Then I ask for the type of the received result, which is an array (Python object). And then, to convert this Python object to a JSON string, I use dumps method. And there you have your JSON.

    >>> res = models.execute_kw(
        db,
        uid,
        password,
        'res.partner',
        'search_read',
        [[
            ['is_company', '=', True],
            ['customer', '=', True]
        ]],
        {
            'fields': ['name', 'country_id', 'comment'],
            'limit': 5
        }
    )
    
    >>> res
    [
        {
            'comment': False,
            'country_id': [69, 'Spain'],
            'id': 1665,
            'name': 'Customer A'
        }, {
            'comment': False,
            'country_id': [69, 'Spain'],
            'id': 5799,
            'name': 'Customer B'
        }, {
            'comment': False,
            'country_id': [69, 'Spain'],
            'id': 1946,
            'name': 'Customer C'
        }, {
            'comment': False,
            'country_id': [69, 'Spain'],
            'id': 1367,
            'name': 'Customer D'
        }, {
            'comment': False,
            'country_id': [69, 'Spain'],
            'id': 2066,
            'name': 'Customer E'
        }
    ]
    
    >>> type(res)
    <type 'list'>
    
    >>> import json
    >>> json.dumps(res)
    
    '[{"comment": false, "country_id": [69, "Spain"], "id": 1665, "name": "CUSTOMER A"}, {"comment": false, "country_id": [69, "Spain"], "id": 5799, "name": "CUSTOMER B"}, {"comment": false, "country_id": [69, "Spain"], "id": 1946, "name": "CUSTOMER C"}, {"comment": false, "country_id": [69, "Spain"], "id": 1367, "name": "CUSTOMER D"}, {"comment": false, "country_id": [69, "Spain"], "id": 2066, "name": "CUSTOMER E"}]'
    
    >>> your_json = json.dumps(res)  # This variable is ready to be sent as JSON
    

    I have updated my answer to clarify it, thanks to @ChesuCR.