如何从 json 字典中的多个嵌套数组中检索相同的键数据?

How to retrieve the same key data from mutiple nested array in json dictionary?

我有嵌套字典格式的数据。我需要检索帐户键中的数据,它本身有一个重复的字典键和值列表。我用相同但不同的值重复 28 次嵌套数组。

[{"report": {
        "accounts": [
          {
            "balance": "0",
            "balancedate": "2021-08-28T00:00:00+00:00",
            "balloonpayment": null,
            "businesstype": "Bank Credit Cards",
            "businesstype_raw": "BC",
            "classification": "REVOLVING",
            "classification_raw": null,
            "companysoldto": null,
            "creditor": {
              "addrln1": "3311 MILL MEADOW DR",
              "addrln2": null,
              "city": "HILLIARD",
              "phone": "BYMAILONLY",
              "state": "OH",
              "subcode": "2250030",
              "zip": "43026"
            },
            "creditorcomments": [
              {
                "commenttext": "Account closed at credit grantor’s request",
                "comment_raw": "18"
              }
            ],
            "creditorcommentsraw": null,
            "dateopened": "2018-04-21T00:00:00+00:00",
            "delinquent30dayscount": 0,
            "delinquent60dayscount": 0,
            "delinquent90plusdayscount": 0,
            "highbalance": null,
            "limit": null,
            "monthlypayment": null,
            "name": "DISCOVER FINANCIAL SVC",
            "number": null,
            "openclosed": "Closed",
            "originalamount": null,
            "originalcreditor": null,
            "pastdueamount": ""},
        {
            "balance": "",
            "balancedate": "2021-07-28T00:00:00+00:00",
            "balloonpayment": null,
            "businesstype": "Bank Credit Cards",
            "businesstype_raw": "BC",
            "classification": "REVOLVING",
            "classification_raw": null,
            "companysoldto": null,
            "creditor": {
              "addrln1": "PO BOX 15369",
              "addrln2": null,
              "city": "WILMINGTON",
              "phone": "8009452000",
              "state": "DE",
              "subcode": "1200320",
              "zip": "19850"
            },
            "creditorcomments": [
              {
                "commenttext": "Account closed at credit grantor’s request",
                "comment_raw": "18"
              }
            ],
            "creditorcommentsraw": null,
            "dateopened": "2020-01-21T00:00:00+00:00",
            "delinquent30dayscount": 0,
            "delinquent60dayscount": 0,
            "delinquent90plusdayscount": 0,
            "highbalance": "40",
            "limit": "11000",
            "monthlypayment": null,
            "name": "BANK CREDIT CARD",
            "number": null,
            "openclosed": "Closed",
            "originalamount": null,
            "originalcreditor": null,
            "pastdueamount": "",
            "paymenthistories": [
              {
         

最终输出字典应如下所示:

{'balance': ["0","",""] #all the data in the nested dict
 'balancedata': ["2021-08-28T00:00:00+00:00",""2021-07-28T00:00:00+00:00""],
 'balloonpayment':[" "]
 so on for all the keys

如果我没有理解错的话,您实际上是在转置数据矩阵。如果没有针对它的三行 pandas 数据框解决方案,我会感到非常惊讶,但纯 python 也可以。


iv = [ {'a':0, 'b':10},{'a':1, 'b':11}]
rv = dict()
for k in iv[0]:
    rv[k]=[i[k] for i in iv]
print( rv)

这是假设所有条目都包含所有键。如果这不成立,您需要添加一些默认值并在第一遍中收集所有键的集合