用python重构JSON数据

2024-05-23 18:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想重新格式化一些JSON数据,以便能够将其传递给pandas/Dash,从而能够创建一个堆叠条形图

json数据可以在这里看到https://ghostbin.co/paste/zzm4w

我所追求的结构如下:

[
   [
      "2020-11-30",
      [
         "2",
         "Jira Server"
      ],
      [
         "1",
         "Jira DataCenter"
      ],
      [
         "7",
         "Jira Cloud"
      ],
      [
         "0",
         "Confluence Server"
      ],
      [
         "0",
         "Confluence DataCenter"
      ],
      [
         "3",
         "Confluence Cloud"
      ],
      [
         "0",
         "Bitbucket Cloud"
      ],
      [
         "0",
         "Bitbucket Server"
      ],
      [
         "0",
         "Bamboo"
      ]
   ],
   [
      "2020-12-01",
      [
         [
            "0",
            "Jira Server"
         ],
         [
            "2",
            "Jira DataCenter"
         ],
         [
            "6",
            "Jira Cloud"
         ],
         [
            "1",
            "Confluence Server"
         ],
         [
            "0",
            "Confluence DataCenter"
         ],
         [
            "0",
            "Confluence Cloud"
         ],
         [
            "0",
            "Bitbucket Cloud"
         ],
         [
            "0",
            "Bitbucket Server"
         ],
         [
            "0",
            "Bamboo"
         ]
      ]
   ]
]

我已经编写了一个函数,它接受json,然后尝试以这种方式构造它,但我最终得到了以下内容,其中包含额外的嵌套层等:

[
   ("2020-11-30",
   [
      [
         [
            "2",
            "Jira Server"
         ]
      ],
      [
         [
            "1",
            "Jira DataCenter"
         ]
      ],
      [
         [
            "7",
            "Jira Cloud"
         ]
      ],
      [
         [
            "0",
            "Confluence Server"
         ]
      ],
      [
         [
            "0",
            "Confluence DataCenter"
         ]
      ],
      [
         [
            "3",
            "Confluence Cloud"
         ]
      ],
      [
         [
            "0",
            "Bitbucket Cloud"
         ]
      ],
      [
         [
            "0",
            "Bitbucket Server"
         ]
      ],
      [
         [
            "0",
            "Bamboo"
         ]
      ]
   ]")",
   "(""2020-12-01",
   [
      [
         [
            "0",
            "Jira Server"
         ]
      ],
      [
         [
            "2",
            "Jira DataCenter"
         ]
      ],
      [
         [
            "6",
            "Jira Cloud"
         ]
      ],
      [
         [
            "1",
            "Confluence Server"
         ]
      ],
      [
         [
            "0",
            "Confluence DataCenter"
         ]
      ],
      [
         [
            "0",
            "Confluence Cloud"
         ]
      ],
      [
         [
            "0",
            "Bitbucket Cloud"
         ]
      ],
      [
         [
            "0",
            "Bitbucket Server"
         ]
      ],
      [
         [
            "0",
            "Bamboo"
         ]
      ]
   ])

这是代码,因为嵌套的for循环,所以很混乱,我才刚刚开始学习列表理解,我觉得这是一种更整洁的方法。 将这些数据重组为所需格式的最佳方法是什么

import json

with open("data.json") as file:
    data = json.load(file)

def builder():
    final_list = []
    dates = [i[0] for i in data[0][1]]
    
    for date in dates:
        date_block = []
        for entry in data:
            hold = []
            for block in entry[1]:
                if date == block[0]:                  
                    obj = [block[1], entry[0]]
                    hold.append(obj)
            date_block.append(hold)
        final_obj = [date, date_block]
        final_list.append(final_obj)        
    print(final_list)
    return final_list   


builder()

Tags: injsoncloudbitbucketfordatadateserver
1条回答
网友
1楼 · 发布于 2024-05-23 18:31:05

首先,列表obj是无用的,因为hold存储了完全相同的数据。
其次,final_obj形成[date, date_block]date_block形成[[names, numbers], ...]。所以final_obj == [date, [[names, numbers], ...]
您所要做的是将datedate_block连接起来,以删除不需要的嵌套,从而拥有[date, [[names, numbers], ...]] -> [date, [names, numbers], ...]

import json
from pprint import pprint

with open("data.json") as file:
    data = json.load(file)

def builder():
    final_list = []
    dates = [i[0] for i in data[0][1]]

    for date in dates:
        date_block = []
        for entry in data:
            for block in entry[1]:
                if date == block[0]:
                    hold = [block[1], entry[0]]
            date_block.append(hold)
            # print(f"{hold=}")
        final_obj = [date] + date_block ### MAGIC LINE
        final_list.append(final_obj)
        # print(f"{final_obj=}")
    pprint(final_list)
    return final_list

builder()

相关问题 更多 >