pythonjsonpython-3.xpython-3.12

Broken Json file in python when decoding


I am making a mod manager which opens the mods manifest.json file for name, description, version and dependencies although when its reading the file... i think ill just show you:

MANIFEST FILE:

{
    "name": "HookGenPatcher",
    "version_number": "0.0.5",
    "website_url": "https://github.com/harbingerofme/Bepinex.Monomod.HookGenPatcher",
    "description": "Generates MonoMod.RuntimeDetour.HookGen's MMHOOK file during the BepInEx preloader phase.",
    "dependencies": []
}

CODE:

from json import JSONDecoder
json = JSONDecoder()

PLUGIN_PATH: str = "./mods"

plugins: dict[str: dict[str: str]] = {}

for mod in os.listdir(path = PLUGIN_PATH):
      mod_path: str = f"{PLUGIN_PATH}/{mod}"
      if is_zipfile(filename = mod_path):
            folder_path: str = ".".join(mod_path.split(sep = ".")[:-1])
            os.mkdir(path = folder_path)

            mod_zip = ZipFile(file = mod_path)
            mod_zip.extractall(path = folder_path)
            mod_zip.close()

            os.remove(path = mod_path)

      with open(file = f"{mod_path}/manifest.json", mode = "r") as f:
            x: list[str] = f.read()
            y: list = []

            for line in x.splitlines():
                  y.append(line.strip())

            y = "".join(y)

            print(y)

            mod_json: dict = json.decode(s = y)

      plugins[mod] = mod_json

print(plugins)

OUTPUT:

{"name": "HookGenPatcher","version_number": "0.0.5","website_url": "https://github.com/harbingerofme/Bepinex.Monomod.HookGenPatcher","description": "Generates MonoMod.RuntimeDetour.HookGen's MMHOOK file during the BepInEx preloader phase.","dependencies": []}

Traceback (most recent call last):
  File "d:\OneDrive\Code\BetterLCMods\main.pyw", line 58, in <module>
    mod_json: dict = json.decode(s = y)
                     ^^^^^^^^^^^^^^^^^^
  File "E:\Python\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\Python\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is only to this manifest file and ive tried deleting it and typing it back out (not copy pasting it)

Im guessing its the "" at the start of the file but I have no idea why that is there.


Solution

  • Best way / Only way is to add this

    y = "{" + y.split("{", 1)[-1]
    

    The whole new file:

    from json import JSONDecoder
    json = JSONDecoder()
    
    PLUGIN_PATH: str = "./mods"
    
    plugins: dict[str: dict[str: str]] = {}
    
    for mod in os.listdir(path = PLUGIN_PATH):
          mod_path: str = f"{PLUGIN_PATH}/{mod}"
          if is_zipfile(filename = mod_path):
                folder_path: str = ".".join(mod_path.split(sep = ".")[:-1])
                os.mkdir(path = folder_path)
    
                mod_zip = ZipFile(file = mod_path)
                mod_zip.extractall(path = folder_path)
                mod_zip.close()
    
                os.remove(path = mod_path)
    
          with open(file = f"{mod_path}/manifest.json", mode = "r") as f:
                x: list[str] = f.read()
                y: list = []
    
                for line in x.splitlines():
                      y.append(line.strip())
    
                y: str = "".join(y)
    
                y = "{" + y.split("{", 1)[-1]
    
                mod_json: dict = json.decode(s = y)
    
          plugins[mod] = mod_json
    
    print(plugins)