Python ValueError: No JSON object could be decoded even though the JSON is valid

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.

Answer

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:
            try:
                _json = json.load(infile)
                if _json.get('enabled'):
                    print('test')
            except Exception as e:
                print(f'Failed to load {entry} -> {e}')