0

Im making a thing for school , and i have no idea how to loop through user keys , my test json is

{
  "Users":{
      "admins":[{"Username":"Showierdata9978","id":4}],
      "Mods":[{"Username":"sssss","id":5}],
      "normal":[{"username":"ssaaa","id":7},{"username":"wwdaw","id":78,{"username":"wadwass","id":9}]
  

   },
  "Data":{
  
   }



 }

What this code is for is a json data saving structure , but i have no idea what future someone testing it will input into the key

current actual code is

    def read(self,io,itemid = None):
      if type(itemid) == type(None):
        if exists(io):
            if pathlib.Path(io).suffix == ".json":
                with open(io) as f :
                    if f.read(1).__str__() == '{':
                        dict1 = j.load(f)
                        for key in dict1:
                           if dict1[key] has keys: #added after for what i want to do
                              loop_again()

                    else :
                        raise NotJsonFormat

            else:
                raise NotJsonFile 
        else:
            raise FileDoesNotExist

in the writing to the file side of it i have this odd code that is writing python to a file thanks to another stack overflow post

self.picode = self.GeneratePyCode(Data, io)
open(
  "DataSaver/DataSaver/CodeGenerator/CustumWrite.py",
  'w').write("")
with open(
    "DataSaver/DataSaver/CodeGenerator/CustumWrite.py",
    'a') as f:

  for NL in self.picode:
    f.write(f"{NL}")
  from .CodeGenerator.CustumWrite import Write
  Write()

def GeneratePyCode(self, data, fp):
    g = gen()
    key = ""
    for a in self.KeyStructure:
        key = f'{a}{key}'
    g += 'import json\n'
    g += 'def Write():\n'
    g += f'\tdict1 = dict(json.load(open("{fp}","r")))'
    g += f'\n\tdict1{key}.update(\n\t\t{data}\n\t\t)\n'
    g += f"\tf = open('{fp}','w')\n"
    g += '\tjson.dump(dict1,f)\n'
  • You don't need to validate the JSON like that. `json.load` will be happy to raise an exception if it's not JSON. Plus, what you have will leave the file pointer pointing at the second byte, so `json.load` will not see that initial '{'. – Tim Roberts Jan 18 '22 at 21:16
  • As the other comment says, use the built in package https://www.w3schools.com/python/python_json.asp then you can treat it like any other dictionary. – ᴓᴓᴓ Jan 18 '22 at 21:18
  • What i'm trying to do is get data from an unknown amount of keys , that can be in another key , infinitely – Showierdata9978 Jan 18 '22 at 21:20
  • You still need to clarify exactly what you're trying to do; simply using `for key in dict1:` will let you loop through unlimited keys. If you want to loop through the `key` within that, you need conditions to check, such as the data type https://stackoverflow.com/questions/25231989/how-to-check-if-a-variable-is-a-dictionary-in-python and from there you can call another loop (again, multiple approaches). – ᴓᴓᴓ Jan 18 '22 at 21:25
  • @wooooooo im trying to get data from json that i have the key holder, but don't know how many there are , and the keys themselves , if you look at the json , there is 2 keys at the start , then in the first key , there are 3 keys , that each hold more data , what i want to do is get those keys and basically get a direct path to it ,also to your comment i have no idea how many layers of json there is – Showierdata9978 Jan 18 '22 at 22:10

0 Answers0