Home > Back-end >  How to retrieve the same key data from mutiple nested array in json dictionary?
How to retrieve the same key data from mutiple nested array in json dictionary?

Time:03-23

I have the data in a nested dictionary format. I need to retreive the data within the accounts key, which itself have a list of repeated dictionary keys and values. I have the nested array repeating 28 times with the same but different values.

[{"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": [
              {
         

The final output dictionary should look something like below:

{'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

CodePudding user response:

If I am understanding you correctly, you are essentially transposing a data matrix. I'd be very surprised if there wasn't a three line pandas dataframe solution for it, but pure python works too.


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)

This is making the assumption that all entries contains all keys. If that doesn't hold, you need to sprinkle in some default values and collect the set of all keys in a first pass

  • Related