Home > Software engineering >  Created multi index nested json or dictionary from list of dictionaries in Python
Created multi index nested json or dictionary from list of dictionaries in Python

Time:08-03

I have the following list of dictionaries :

"owners_data": [
{
  "owner_id":1,
  "in_hours": 30,
  "us_hours": 1,
  "client_id": "139",
  "in_revenue": 0,
  "in_te_cost": 0,
  "project_id": "100336",
},
{
  "owner_id":1,
  "in_hours": 30,
  "us_hours": 1,
  "client_id": "139",
  "in_revenue": 0,
  "in_te_cost": 0,
  "project_id": "100337",
},
{
  "owner_id":1,
  "in_hours": 30,
  "us_hours": 1,
  "client_id": "139",
  "in_revenue": 0,
  "in_te_cost": 0,
  "project_id": "100338",
},
{
  "owner_id":2,
  "in_hours": 30,
  "us_hours": 1,
  "client_id": "129",
  "in_revenue": 0,
  "in_te_cost": 0,
  "project_id": "100339",
},
{
  "owner_id":2,
  "in_hours": 30,
  "us_hours": 1,
  "client_id": "149",
  "in_revenue": 0,
  "in_te_cost": 0,
  "project_id": "100343",
}

Now I need to convert it to a nested JSON or dictionary so that the values are grouped under the corresponding owner, client, and project. So the output looks like

  "owners_data": {
    "1":{
       "139":{
            "100336":{
               "in_hours": 30,
               "us_hours": 1,
               "in_revenue": 0,
               "in_te_cost": 0,,
            },
            "100337":
            {
                "in_hours": 32,
                "us_hours": 1,
                "in_revenue": 100,
                "in_te_cost": 192,
            },
            "100338":
            {
                "in_hours": 30,
                "us_hours": 1,
                "in_revenue": 1000,
                "in_te_cost": 33,
            }
       }
    },
    {
    "2" {
        "100339":{
            "in_hours": 30,
            "us_hours": 1,
            "in_revenue": 1000,
            "in_te_cost": 33,
        },
        "100343":{
            "in_hours": 30,
            "us_hours": 1,
            "in_revenue": 1000,
           "in_te_cost": 33,
        }
    }
    }
    }

The above is not the correct JSON, but I need it in the correct format with indexes as the corresponding fields. How to do this?

I am trying the following:

output = defaultdict(dict)
for data in acc_owner_data:
    output[int(data['acc_owner_id'])][int(data['client_id'])] = data
print(dict(output))
exit()

But this is only showing one project for the client eventhough there is multiple projects. If I try to add the projec_id there, like output[int(data['acc_owner_id'])][int(data['client_id'])][int(data['project_id'])] it is showing KeyError: 139

How this can be fixed ?

CodePudding user response:

I guess you can fix it by using nested defaultdict:

import collections

def tree():
    return collections.defaultdict(tree)


root = tree()
for obj in adict['owners_data']:
    owner_id = obj.pop('owner_id')
    client_id = obj.pop('client_id')
    project_id = obj.pop('project_id')
    root[owner_id][client_id][project_id] = obj

import json

print(json.dumps(root))
  • Related