Python 展开嵌套 json

Python expand nested json

我需要扩展下面的json:

data = [
   'elements': {
      "totalPageStatistics":{
         "clicks":{
            "mobileCustomButtonClickCounts":[
               {
                  "customButtonType":"VIEW_WEBSITE",
                  "clicks":0
               }
            ],
            "careersPageClicks":{
               "careersPagePromoLinksClicks":0,
               "careersPageBannerPromoClicks":0,
               "careersPageJobsClicks":0,
               "careersPageEmployeesClicks":0
            },
            "desktopCustomButtonClickCounts":[
               {
                  "customButtonType":"VIEW_WEBSITE",
                  "clicks":0
               }
            ],
            "mobileCareersPageClicks":{
               "careersPageJobsClicks":0,
               "careersPagePromoLinksClicks":0,
               "careersPageEmployeesClicks":0
            }
         },
         "views":{
            "mobileProductsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "allDesktopPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "insightsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileAboutPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "allMobilePageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopProductsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "productsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "jobsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "peoplePageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "overviewPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileOverviewPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "lifeAtPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopOverviewPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileCareersPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "allPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileJobsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "careersPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileLifeAtPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopJobsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopPeoplePageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "aboutPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopAboutPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobilePeoplePageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopInsightsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopCareersPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "desktopLifeAtPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            },
            "mobileInsightsPageViews":{
               "pageViews":0,
               "uniquePageViews":0
            }
         }
      },
      "timeRange":{
         "start":1640995200000,
         "end":1641081600000
      },
      "organization":"urn:li:organization:111111111"
   }
]

这是我的代码:

(1) 选项:


flat_data = json_normalize(data['elements'])
flat_data.rename(dict((x, x.split('.')[-1]) for x in flat_data.columns if '.' in x), axis=1, inplace=True)
flat_data

(2) 使用 flatten_json

flat = flatten_json(data)
flat_data = json_normalize(flat)

选项 (1) 效果更好:

但是,我不知道展开列(红色屏幕截图)。拜托,有没有人可以向我解释如何做到这一点? 谢谢

编辑: 下面是 table 应该是什么样子的示例。

使用 flatten_json 库我得到了这个(一个独特的行),我发现 hark 以这种方式工作。

我已经解决了我的问题:

  1. 展开 2 列
  2. 与主 df 连接
  3. 删除原始列
flat_data = json_normalize(data['elements'])
dd = flat_data.explode(['totalPageStatistics.clicks.mobileCustomButtonClickCounts', 'totalPageStatistics.clicks.desktopCustomButtonClickCounts']).reset_index()
ff = pd.concat([pd.DataFrame(dd), 
                json_normalize(dd['totalPageStatistics.clicks.mobileCustomButtonClickCounts']),
                json_normalize(dd['totalPageStatistics.clicks.desktopCustomButtonClickCounts']),
                   ], 
                axis=1).drop(columns=['totalPageStatistics.clicks.mobileCustomButtonClickCounts', 'totalPageStatistics.clicks.desktopCustomButtonClickCounts'])
ff

这不是一个理想的解决方案,但至少我有一些格式可以处理数据。