Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

postgresql - 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,
                    })
question from:https://stackoverflow.com/questions/65863655/get-value-in-nested-dictionary-python-odoo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...