-2

I would like to create a JSON like object (nested dictionary structure) in Python and I wondered if I could do this in a more simplified (directly assining and creating the sublevels of the dictionary on the fly) manner then creating a dictionary, a list in a dictionary and appending dictionaries to it.

Something like:

my_json={}
my_json['first']['second']['third']=array_123
MaxS
  • 978
  • 3
  • 17
  • 34
  • Hi MaxS, could you clarify what you mean by "more simplified" and "JSON like object"? Also, what is the source of your object's content? – David Z Oct 06 '20 at 06:39
  • 1
    Does this answer your question? [defaultdict of defaultdict?](https://stackoverflow.com/questions/5029934/defaultdict-of-defaultdict) – quamrana Oct 06 '20 at 06:45
  • @quamrana yes, this seems to be helpful. defaultdict(lambda:defaultdict(defaultdict)) – MaxS Oct 06 '20 at 07:01

1 Answers1

1

You can convert a JSON string (it can be loaded from file or net etc.) to a dictionary by json module.

import json
j = """{
  "first": {
    "second": {
      "third": [1, 2, 3, 4, 5]
    }
  }
}"""
my_json = json.loads(j)
print(my_json['first']['second']['third'])
F.NiX
  • 1,457
  • 3
  • 12
  • 20