Search code examples
python-3.xpostgresqlodoo

Get value in Nested Dictionary Python Odoo


I have a problem...again. It is related to my previous question in Cron. I've got JSON value and I want to enter it in database. I need help in getting values in this nested dict. Plz help!

JSON

{'folders': [{'id': 94, 'name': 'Retargeting January 2021', 'totalBlacklisted': 606, 'uniqueSubscribers': 19988, 'totalSubscribers': 19382}, 
{'id': 90, 'name': 'Leads', 'totalBlacklisted': 0, 'uniqueSubscribers': 0, 'totalSubscribers': 0},
{'id': 84, 'name': 'Retargeting Year End', 'totalBlacklisted': 1367, 'uniqueSubscribers': 18847, 'totalSubscribers': 17480}, 
{'id': 79, 'name': 'CRM Folder', 'totalBlacklisted': 0, 'uniqueSubscribers': 3, 'totalSubscribers': 3},
{'id': 56, 'name': 'Curioo P', 'totalBlacklisted': 282, 'uniqueSubscribers': 3279, 'totalSubscribers': 2997}]}

Python

res = simplejson.loads(response.text)
self.env['get.folders'].create({
            'id' : self.id,
            'name': res['name'],
            'email_blacklist': res['totalBlacklisted'],
            'email_subscribers': res['totalSubscribers'],
            'unique_subscribers': res['uniqueSubscribers'],
            'foldersId': res['id'],
            })

EDIT At last it works. I try to spell out the values and I don't know how but it works this way. Thanks @Jack Dane for your help.

for folder in folders.get("folders"):
            names = folder['name']
            ids = folder['id']
            blacklist = folder['totalBlacklisted']
            subscribe = folder['totalSubscribers']
            unique = folder['uniqueSubscribers']
            self.env['sendinblue.get_folders'].create({
                    # 'id' : folder['id'],
                    'name_folder': names,
                    'email_blacklist': blacklist,
                    'email_subscribers': subscribe,
                    'unique_subscribers': unique,
                    'foldersId': ids,
                    })

Solution

  • You can loop through the folders using a foreach loop call the create function:

    folders = {'folders': [{'id': 94, 'name': 'Retargeting January 2021', 'totalBlacklisted': 606, 'uniqueSubscribers': 19988, 'totalSubscribers': 19382}, 
    {'id': 90, 'name': 'Leads', 'totalBlacklisted': 0, 'uniqueSubscribers': 0, 'totalSubscribers': 0},
    {'id': 84, 'name': 'Retargeting Year End', 'totalBlacklisted': 1367, 'uniqueSubscribers': 18847, 'totalSubscribers': 17480}, 
    {'id': 79, 'name': 'CRM Folder', 'totalBlacklisted': 0, 'uniqueSubscribers': 3, 'totalSubscribers': 3},
    {'id': 56, 'name': 'Curioo P', 'totalBlacklisted': 282, 'uniqueSubscribers': 3279, 'totalSubscribers': 2997}]}
    
    for folder in folders.get("folders"):
        self.env['get.folders'].create({
                'id' : self.id,
                'name': folder['name'],
                'email_blacklist': folder['totalBlacklisted'],
                'email_subscribers': folder['totalSubscribers'],
                'unique_subscribers': folder['uniqueSubscribers'],
                'foldersId': folder['id'],
                })
    

    In my case, I have used folders as the variable which will be returned as a JSON.

    If you need any clarification let me know,

    Thanks,