I have this very short JSON-code in a “utf-8 without BOM”-encoded file:
{ "paths": ["A:\\path\\to\\dir"],
"anotherPath": "os.path.join(os.path.dirname( __file__ ), '..')"
}
I ensured its validity with different online JSON validators.
But with following Python code…
jsonfile = "working\\path\\to\\myProgram.conf"
with open(jsonfile) as conf:
confContent = json.load(conf)
# doStuff...
… I receive this error:
No JSON object could be decoded
I know that the path is correct, because I read its content successfully at a different place.
Any ideas what could be wrong?
The problem is that you don’t actually have a file encoded as UTF-8 without BOM.
You can generate a file with that string encoded that way as follows:
(Whether the
'b'is necessary or not depends on whether you’re on Python 2 or 3, and whether you’re on Windows or Unix. But if it’s not necessary, it’s harmless.)Now, if you run your code against that file, it works.
But you can compare the
test.jsonfile to yourworking\\path\\to\\myProgram.conffile and see what the difference is. (Most non-Windows systems come with command-line tools likehexdump; on Windows you may have to get a little fancier to spot the differences.)