json.load causing IndexError: list index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: json.load causing IndexError: list index out of range (/thread-18474.html) |
json.load causing IndexError: list index out of range - WallyZ - May-19-2019 I'm trying to debug the ubuntu-mate-welcome python script which is refusing to load a valid json file. I have validated the json file with jsonlint. However my experience with python is limited. My environment is Ubuntu 18.04 aarch64 (arm64). I have isolated the problem code in an example included here: #! /usr/bin/python3 import sys import json print(sys.path) json_path = '' try: print('Reading index...') json_path = '/usr/share/ubuntu-mate-welcome/js/applications.json' with open(json_path) as data_file: print('Loading JSON.') index = json.load(data_file) print('LOADED JSON!!!!') except Exception as e: index = None print('Software Index JSON is invalid or missing!') print(json_path)The output is as follows: However my example does not display the : So I am not certain I have faithfully reproduced the problem!Output from jsonlint-php: I have looked in the python path but cannot identify the json module referred to in the import statement.How can I be certain I have the json module installed? RE: json.load causing IndexError: list index out of range - buran - May-19-2019 First step in this case is to remove the try/except. At the moment you catch all exceptions. Then post full traceback you get as well as the json file itself. (May-19-2019, 01:45 PM)WallyZ Wrote: How can I be certain I have the json module installed?it's part of the python standard library, so you have it RE: json.load causing IndexError: list index out of range - DeaD_EyE - May-19-2019 raise the exception again to see where the error occurs. You could try https://github.com/codecobblers/dirtyjson I downloaded the package and extracted the file: https://packages.ubuntu.com/xenial/all/ubuntu-mate-welcome I am able to load the json with Python 3.7.3 You can also upload your broken applications.json. RE: json.load causing IndexError: list index out of range - WallyZ - May-19-2019 Ok after removing the try/except I now get a different picture: Does that mean there is a strange character in the json file?Found some accented characters in the json file. The application Deja had accented e and a. I changed them to normal a nad e and the json now loads. This is the standard applications.json for the ubuntu-mate-welcome package which loads OK on my Ubuntu 18.04 Mate Raspberry PI system. So why does my code fail on one system and succeed on the other when both applications.json files are the same? So is there some python setting I can set to make accented characters acceptable? Why are accented characters acceptable on my Ubuntu 18.04 PI but unacceptable on my Ubuntu 18.04 Odroid? RE: json.load causing IndexError: list index out of range - DeaD_EyE - May-19-2019 The file seems to be corrupt. Usually json saves unicode chars with the prefix \u followed by a hex number. If the character is encoded with utf8 (no \u notation), the normal json module is still able to load and decode it. But if there for example quotes are missing, this can lead into this strange exception. You could try the dirtyjson, load the string and dump it with the python-json module. Then you can compare the original file together with the corrected fie (from json.dump or json.dumps). RE: json.load causing IndexError: list index out of range - WallyZ - May-20-2019 Thanks for all the advise. I did test the json file in both jsonlint and dirty json. Both tools stated the json was valid. Here is snippet of the json file where the accented characters are found: "deja-dup": { "name": "Déjà Dup", "img": "deja-dup", "main-package": "deja-dup", "launch-command": "deja-dup", I just decide to replace the accented character in the end. I thought it may have been a python 2 versus 3 issue but I tried both versions. The script doesn't appear to work at all in version 2 so I was suprised that the accented characters caused an issue in version 3. |