Convert nested Python dictionary to object Last Updated : 28 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: # constructor def __init__(self, dict1): self.__dict__.update(dict1) def dict2obj(dict1): # using json.loads method and passing json.dumps # method and custom object hook as arguments return json.loads(json.dumps(dict1), object_hook=obj) # initializing the dictionary dictionary = {'A': 1, 'B': {'C': 2}, 'D': ['E', {'F': 3}],'G':4} # calling the function dict2obj and # passing the dictionary as argument obj1 = dict2obj(dictionary) # accessing the dictionary as an object print (obj1.A) print(obj1.B.C) print(obj1.D[0]) print(obj1.D[1].F) print(obj1.G) Output1 2 E 3 4 Time complexity: O(n), where n is the total number of elements in the input dictionary.Auxiliary space: O(n), where n is the total number of elements in the input dictionary Method 2: Using the isinstance() method We can solve this particular problem by using the isinstance() method which is used to check whether an object is an instance of a particular class or not. Python3 def dict2obj(d): # checking whether object d is a # instance of class list if isinstance(d, list): d = [dict2obj(x) for x in d] # if d is not a instance of dict then # directly object is returned if not isinstance(d, dict): return d # declaring a class class C: pass # constructor of the class passed to obj obj = C() for k in d: obj.__dict__[k] = dict2obj(d[k]) return obj # initializing the dictionary dictionary = {'A': 1, 'B': {'C': 2}, 'D': ['E', {'F': 3}],'G':4} # calling the function dict2obj and # passing the dictionary as argument obj2 = dict2obj(dictionary) # accessing the dictionary as an object print(obj2.A) print(obj2.B.C) print(obj2.D[0]) print(obj2.D[1].F) print(obj2.G) Output1 2 E 3 4 Comment More infoAdvertise with us Next Article How To Convert Generator Object To Dictionary In Python M manandeep1610 Follow Improve Article Tags : Python python-dict Practice Tags : pythonpython-dict Similar Reads Python - Convert Dictionary Object into String In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this:Using strThe simplest way to conver 2 min read Convert Lists to Nested Dictionary - Python The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = ["gfg", "is", "best"], b = ["ratings", "price", "score"], and c = [5, 6, 7], the g 3 min read Convert JSON to dictionary in Python JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Pytho 4 min read Convert a Nested OrderedDict to Dict - Python The task of converting a nested OrderedDict to a regular dictionary in Python involves recursively transforming each OrderedDict including nested ones into a standard dictionary. This ensures that all OrderedDict instances are replaced with regular dict objects, while maintaining the original struct 3 min read How To Convert Generator Object To Dictionary In Python We are given a generator object we need to convert that object to dictionary. For example, a = (1, 2, 3), b = ('a', 'b', 'c') we need to convert this to dictionary so that the output should be {1: 'a', 2: 'b', 3: 'c'}.Using a Generator ExpressionA generator expression can be used to generate key-val 3 min read Python - Convert list of dictionaries to JSON Converting a list of dictionaries to JSON in Python involves serializing Python objects into a lightweight, human-readable JSON format used in APIs, storage and data exchange. For example, converting employee records stored as a list of dictionaries into a JSON structure allows easy data exchange or 3 min read Like