我有以下类型的数据,其中dict有嵌套dict,还有一些嵌套列表
Input
{
"properties": {
"property": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman",
"budgets": {
"budget": [
{
"id": "32133",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12",
"glAccounts": {
"glAccount": [
{
"id": 159475,
"accountName": "Residential Rental Revenue",
"accountNumber": "40000",
"glTotals": {
"glTotal": [
{
"amount": 3880956
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 321888
},
{
"month": "02/01/2020",
"amount": 321888
}]
}
},{
"id": 242922,
"accountName": "Parking Revenue",
"accountNumber": "40100",
"glTotals": {
"glTotal": [
{
"amount": 14760
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 1230
},
{
"month": "02/01/2020",
"amount": 1230
}
]
}
}
]
}
},
{
"id": "12299",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12",
"glAccounts": {
"glAccount": [
{
"id": 159475,
"accountName": "Residential Rental Revenue",
"accountNumber": "40000",
"glTotals": {
"glTotal": [
{
"amount": 3880956
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 321888
},
{
"month": "02/01/2020",
"amount": 321888
}]
}
},{
"id": 242922,
"accountName": "Parking Revenue",
"accountNumber": "40100",
"glTotals": {
"glTotal": [
{
"amount": 14760
}
]
},
"budgetMonths": {
"budgetMonth": [
{
"month": "01/01/2020",
"amount": 1230
},
{
"month": "02/01/2020",
"amount": 1230
}
]
}
}
]
}
}
]
}
}
]
}
}
我必须将以上数据转换成以下格式。我尝试了很多方法,但都没有成功。也尝试过使用Fattern lib,但它只对初始嵌套进行了修饰。我需要找到一种动态的方式
Output
{
"properties": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman"
}
],
"budgets": [
{
"propertyId": "508276",
"id": "32133",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12"
},
{
"propertyId": "508276",
"id": "12299",
"budgetName": "Norman 2020 Budget",
"budgetStatusTypeId": "1",
"isDefault": "1",
"fiscalStartDate": "01/01/2020",
"fiscalEndDate": "12/31/2020",
"fiscalYear": "2020",
"budgetTotal": "-88245.12"
}
],
"glAccounts": [
{
"id": 159475,
"budgets_id": "32133",
"accountName": "Residential Rental Revenue",
"accountNumber": "40000"
},
{
"id": 242922,
"budgets_id": "32133",
"accountName": "Parking Revenue",
"accountNumber": "40100"
},
{
"id": 212343,
"budgets_id": "12299",
"accountName": "Residential Rental Revenue",
"accountNumber": "34343"
},
{
"id": 455454,
"budgets_id": "12299",
"accountName": "Parking Revenue",
"accountNumber": "32323"
}
],
"glTotals": [{some data list...}],
"budgetMonths": [{some data list...}]
}
Python Code that I have used to solve this
from flatten_dict import flatten
#'inp' is the input which I have mentioned above
flat = flatten(inp, reducer=lambda k1, k2: k2 if k1 is None else k1 + '.' + k2)
print(flat)
#Output from this code
{
"properties.property": [
{
"propertyId": "508276",
"propertyName": "State On Campus Norman",
"budgets": {
"budget": [.......
请使用python帮助解决此问题
这个问题的解决方案
相关问题 更多 >
编程相关推荐