0

I have a json response in the format below.

Using Python 2.7.5 (I have no control over the python version), I am interested in getting the category name, provider name, branch name (for now). The challenge is that the response is heavily nested and the objects use the same keys (for instance all have 'Id' and 'Name'. So when I extract the 'Name' values I get all instances of Names which is hard to process.

data = 
[
    {
        "Id": 1,
        "Name": "Utility",
        "Category": "Utilities",
        "Providers": [
            {
                "Id": 1,
                "Name": "Electricity Company",
                "Someotherdets": "Elec",
                "Location": {
                    "Name": "City Location 1",
                    "Id": 1,
                    "State": "StateName"
                },
                "Branches": [
                    {
                        "Id": 1,
                        "Name": "Elec Branch 1",
                        "Street": "Str 1",
                        "Open": true,
                        "branchDets": []
                    },
                    {
                        "Id": 2,
                        "Name": "Elec Branch 2",
                        "Street": "Str 2",
                        "Open": true,
                        "branchDets": [
                            {
                                "Id": 1,
                                "OtherStuff": "abcd@example.com"
                            }
                        ]
                    }
                ]
            },
            {
                "Id": 1,
                "Name": "Water Company",
                "Someotherdets": "Water",
                "Location": {
                    "Name": "City Location 1",
                    "Id": 1,
                    "State": "StateName",
                },
                "Branches": [
                    {
                        "Id": 1,
                        "Name": "Water Branch 1",
                        "Street": "Str 1",
                        "Open": true,
                        "branchDets": []
                    },
                    {
                        "Id": 2,
                        "Name": "Water Branch 2",
                        "Street": "Str 2",
                        "Open": true,
                        "branchDets": []
                    }
                ]
            },
        ],
    },
    {
        "Id": 2,
        "Name": "FinInstitutions",
        "Category": "Banks",
        "Providers": [
            {
                "Id": 1,
                "Name": "Bank A",
                "Someotherdets": "Bank A details",
                "Location": {
                    "Name": "City Location 1",
                    "Id": 1,
                    "State": "StateName"
                },
                "Branches": [
                    {
                        "Id": 1,
                        "Name": "Bank A Branch 1",
                        "Street": "Str 1",
                        "Open": true,
                        "branchDets": [
                            {
                                "Id": 1,
                                "OtherStuff": "etc"
                            }
                        ]
                    },
                    {
                        "Id": 2,
                        "Name": "Bank A Branch 2",
                        "Street": "Str 2",
                        "Open": true,
                        "branchDets": []
                    }
                ]
            },
            {
                "Id": 2,
                "Name": "Bank 2",
                "Someotherdets": "Bank 2 Branches",
                "Location": {
                    "Name": "City Location 1",
                    "Id": 1,
                    "State": "StateName",
                },
                "Branches": [
                    {
                        "Id": 1,
                        "Name": "Bank 2 Branch 1",
                        "Street": "Str 1",
                        "Open": true,
                        "branchDets": [
                            {
                                "Id": 1,
                                "OtherStuff": "etc"
                            }
                        ]
                    },
                    {
                        "Id": 2,
                        "Name": "Bank 2 Branch 2",
                        "Street": "Str 2",
                        "Open": true,
                        "branchDets": []
                    }
                ]
            },
        ],
    }
]

What I have shared is heavily summarized. The actual response is over 100kb and 5000+ lines long when formatted.

I tried doing

for i in data['Provider'].keys:
  for y in data['Provider'][i]:
  print data['Provider'][i][y]
  ...

but I get the TypeError: list indices must be integers, not str error

The end product should be a listing in the format below (I have used pipe character as a delimiter for illustration purposes only)

CategoryID|CategoryName|ProviderID|ProviderName|BranchName|BranchOpenStatus|BranchDetsOtherStuff
1|Utility|1|Electric Company|Elec Branch 1|True|null
1|Utility|1|Electric Company|Elec Branch 2|True|abcd@example.com
1|Utility|2|Water Company|Water Branch 1|True|null
2|FinInstitutions|1|Bank A|Bank A Branch 1|True|null
2|FinInstitutions|1|Bank B|Bank B Branch 1|True|null
masterl
  • 141
  • 1
  • 2
  • 10

0 Answers0