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



我有嵌套字典格式的数据。我需要检索accounts关键字中的数据,它本身有一个重复的字典关键字和值的列表。我让嵌套数组用相同但不同的值重复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)

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

最新更新