Home > Back-end >  Making Nested JSON Python 3
Making Nested JSON Python 3

Time:05-05

I want to dump a Nested JSON data into a file, the format as below :

PlaceCode, AreaCode, SchoolCode, IdentityCode will get append new in the future

Data[PlaceCode] = {
                    "PlaceName": PlaceName,
                    "Areas": {
                        AreaCode: {
                            "AreaName": AreaName,
                            "Schools": {
                                SchoolCode: {
                                    "SchoolName": SchoolName,
                                    "Students": {
                                        IdentityCode: {
                                            "StudentName": StudentName
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

I had made a script as below, but it is a wrong code, any ways to have a more better script?

while True :
    try :
        Data[PlaceCode]["Areas"]
        
        try :
            Data[PlaceCode]["Areas"][AreaCode]
            
            try :
                Data[PlaceCode]["Areas"][AreaCode]["Schools"]
                
                try :
                    Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]
                    
                    try :
                        Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]["Students"]
                        
                        try :
                            Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]["Students"][IdentityCode]
                            
                            break
                            
                        except KeyError :
                            Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]["Students"] = {IdentityCode : {"StudentName" : StudentName}}
                            
                    except KeyError :
                        Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode] = {"SchoolName" : SchoolName,
                                                                                        "Students" : None}
                        
                except KeyError :
                    Data[PlaceCode]["Areas"][AreaCode]["Schools"] = {SchoolCode : None}
                    
            except KeyError :
                Data[PlaceCode]["Areas"][AreaCode] = {"AreaName" : AreaName,
                                                        "Schools" : None}
        
        except KeyError :
            Data[PlaceCode]["Areas"] = {AreaCode : None}
            
    except KeyError :
        Data[PlaceCode] = {"PlaceName" : PlaceName,
                            "Areas" : None}


json.dump (Data, open("Final.json", "w"), indent = 4, ensure_ascii = False, sort_keys = False)

CodePudding user response:

I made these, it works btw

if PlaceCode not in Data.keys() :
    Data[PlaceCode] = {
        "PlaceName": PlaceName,
        "Areas": {
            AreaCode: {
                "AreaName": AreaName,
                "Schools": {
                    SchoolCode: {
                        "SchoolName": SchoolName,
                        "Students": {
                            IdentityCode: {
                                "StudentName": StudentName
                            }
                        }
                    }
                }
            }
        }
    }
    
if AreaCode not in Data[PlaceCode]["Areas"].keys() :
    Data[PlaceCode]["Areas"][AreaCode] = {
        "AreaName": AreaName,
        "Schools": {
            SchoolCode: {
                "SchoolName": SchoolName,
                "Students": {
                    IdentityCode: {
                        "StudentName": StudentName
                    }
                }
            }
        }
    }

if SchoolCode not in Data[PlaceCode]["Areas"][AreaCode]["Schools"] :
    Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode] = {
        "SchoolName": SchoolName,
        "Students": {
            IdentityCode: {
                "StudentName": StudentName
            }
        }
    }

if IdentityCode not in Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]["Students"] :
    Data[PlaceCode]["Areas"][AreaCode]["Schools"][SchoolCode]["Students"][IdentityCode] = {
        "StudentName": StudentName
    }

CodePudding user response:

Consider your data structure and how you want to update it. Something like this should help:

Data = {}


def insert(keys, mapping):
    _insert(Data, keys, mapping)


def _insert(data, keys, mapping):
    [key, *keys] = keys
    if key not in data:
        data[key] = {}
    if keys:
        _insert(data[key], keys, mapping)
    else:
        data[key] = {**data[key], **mapping}

In [2]: insert([1], {'place_name': 'pc1'})                                                                                                  

In [3]: Data                                                                                                                                
Out[3]: {1: {'place_name': 'pc1'}}

In [4]: insert([1, "Areas", 1], {'area_name': 'a1'})                                                                                        

In [5]: Data                                                                                                                                
Out[5]: {1: {'place_name': 'pc1', 'Areas': {1: {'area_name': 'a1'}}}}

In [6]: insert([1, "Areas", "Schools", 1], {'school_name': 's1', "Students": {1: {"student_name": 'tom'}, 2: {"student_name": 'dick'}}})    

In [7]: Data                                                                                                                                
Out[7]: 
{1: {'place_name': 'pc1',
  'Areas': {1: {'area_name': 'a1'},
   'Schools': {1: {'school_name': 's1',
     'Students': {1: {'student_name': 'tom'}, 2: {'student_name': 'dick'}}}}}}}

However, given the nature of the data you are building you may want to investigate using a database such as sqlite before going too much further.

  • Related