Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a valid JSON file (verified with this website) that all of sudden can't be read.

This script has been working since forever. I'm not sure what changed.

This is the script:

import os
import json
basepath = '/path/'
for entry in os.listdir(basepath):
    if os.path.isfile(os.path.join(basepath, entry)):
        with open("{}/{}".format(basepath, entry), 'r') as myfile:
            data=myfile.read()
        obj = json.loads(data)
        if obj.get('enabled'):
            print("test")

This is the full error:

File "src/utils/process_utils.py", line 34, in invoke
    raise ExecutionException(result_code, error, output)
utils.process_utils.ExecutionException: Execution failed. Code 1: Traceback (most recent call last):
  File "conf/scripts/script.py", line 12, in <module>
    obj = json.loads(data)
  File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I use read() and loads to make sure everything is a string. Looking for ideas on what could have changed, because this script has been working since forever and it's automated.

Edit:

Just tried without read(). Just json.load and it gives the same output.

Edit2:

An example for the JSON file:

"id": 1234567, "login": "jsmith", "name": "John Smith", "first_name": "John",

The rest of the file just looks the same, more parameters about users.

find . -not -name "*.json" didn't find non-JSON files.

printing the data variable:

John Smith (jsmith)
    "department_number": 123,
    "department_name": "Department",
    "anpa_dep": "Dep",
    "requester": nul
Traceback (most recent call last):
  File "conf/scripts/script-server_anpa_users.py", line 13, in <module>
    obj = json.loads(data)
  File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

the "requester": nul looks weird. Gonna try to remove that specific json.

This code will try load the contents of all/any files in the basepath. Perhaps they're not all JSON files – user2668284 Aug 15, 2021 at 7:51

It appears from the comments that only files with a .json suffix are to be considered. On that basis I propose this more robust approach:-

import glob
import json
import os
if __name__ == '__main__':
    base = '/path'
    for entry in glob.glob(os.path.join(base, '*.json')):
        with open(entry) as infile:
                _json = json.load(infile)
                if _json.get('enabled'):
                    print('test')
            except Exception as e:
                print(f'Failed to load {entry} -> {e}')

Looks like there was a rouge .json file that wasn't parsing correctly. I printed the data variable and saw that some json file was getting stuck. Weird...

Thanks for the help.

basepath = '/path/' for entry in os.listdir(basepath): if os.path.isfile(os.path.join(basepath, entry)): with open("{}/{}".format(basepath, entry), 'r') as myfile: data=myfile.read() print(data) obj = json.loads(data) except ValueError: # this will point to the problematic file file_name = "{}/{}".format(basepath, entry) print('file {} is not a valid json'.format(file)) if obj.get('enabled'): print("test") JSONDecodeError appears to be sub-classed from ValueError. However, that's not documented and could therefore change in a future release of Python – user2668284 Aug 15, 2021 at 9:00

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.