将嵌套的json转换为DF

时间:2019-08-18 08:23:39

标签: json

我想将此嵌套json转换为df。有嵌套列表和字典的两列,我无法正确解析。

我需要解析嵌套的列(类别和displayNames)并将其与我的整个DF正确组合。

An example of the df before parsing the nested columns

{'entities': [{'category': {'canBeCombined': True,
                            'canBeCombinedTo': True,
                            'canBeRedirected': True,
                            'canBeRedirectedTo': True,
                            'canBeRolledUp': False,
                            'canBeRolledUpTo': False,
                            'canBecomeParent': True,
                            'canHoldInventory': True,
                            'categoryCondition': 'NONE',
                            'categoryStatus': 'ACTIVE'
                            'changeTimestamp': 1560370490980,
                            'combinedFromCategories': None,
                            'combinedTo': False,
                            'combinedToCategory': None,
                            'creationTimestamp': 956102400000,
                            'description': None,
                            'domainName': None,
                            'entityProperties': None,
                            'entityState': None,
                            'entityType': 'C',
                            'flags': None,
                            'parentCategoryGuid': None,
                            'parentCategoryId': None,
                            'redirectedTo': False,
                            'rolledUpTo': False,
                            'score': 0,
                            'site': {'additionalLocales': ['ru-RU',
                                                           'pt-BR',
                                                           'es-CO'],
                                     'changeTimestamp': 1560369861482,
                                     'creationTimestamp': None,
                                     'entityState': None,
                                     'entityType': 'ST',
                                     'guid': 'ST_1',
                                     'id': 0,
                                     'locale': 'en-US',
                                     'maintainedSites': None,
                                     'name': 'United States',
                                     'shortName': 'US',
                                     'treeVersion': 5},
                            'siteId': 22,
                            'sourceCategoryTemplate': None,
                            'terminationMode': 'NONE',
                            'weight': '1000.0'},
               'changeTimestamp': 1561499100827,
               'creationTimestamp': 1561499100827,
               'dependencyParent': None,
               'description': None,
               'displayNames': [{'changeTimestamp': 1555084498504,
                                 'creationTimestamp': 1555084498504,
                                 'defaultSynonym': True,
                                 'entityState': None,
                                 'guid': 'kk',
                                 'locale': 'en-US',
                                 'synonym': 'l'},
                                {'changeTimestamp': 1555085132942,
                                 'creationTimestamp': 1555085132942,
                                 'defaultSynonym': True,
                                 'entityState': None,
                                 'entityType': 'GAS',
                                 'globalAttributeGuid': None,
                                 'globalAttributeName': None,
                                 'guid': 'll',
                                 'locale': 'es-CO',
                                 'synonym': 'Marca'},
                                {'changeTimestamp': 1555085132942,
                                 'creationTimestamp': 1555085132942,
                                 'defaultSynonym': True,
                                 'entityState': None,
                                 'entityType': 'GAS',
                                 'globalAttributeGuid': None,
                                 'globalAttributeName': None,
                                 'guid': 'lll',
                                 'locale': 'ru-RU',
                                 'synonym': 'Бренд'},
                                {'changeTimestamp': 1555085132942,
                                 'creationTimestamp': 1555085132942,
                                 'defaultSynonym': True,
                                 'entityState': None,
                                 'entityType': 'GAS',
                                 'globalAttributeGuid': None,
                                 'globalAttributeName': None,
                                 'guid': 'gg',
                                 'locale': 'pt-BR',
                                 'synonym': 'Marca'}],
               'entityProperties': None,
               'entityState': 'UNCHANGED',
               'entityType': 'CA',
               'globalAttribute': None,
               'guid': 'ggd',
               'requiredTranslations': None,
               'tagType': 'BOTH'}
with open(r"C:\\Downloads\response_1566112285719.json",encoding='utf-8-sig') as f:
    data = json.load(f)
    pprint(data)

df = pd.DataFrame.from_dict(data['entities'])

我想学习解析json背后的逻辑,即时消息可以处理许多API请求。 因此,如果有人对如何操作有一些建议或一个很好的指南(可以在网上找到任何建议)

0 个答案:

没有答案