Tuesday, 15 May 2012

python - Valid JSON, but "No JSON object could be decoded" -



python - Valid JSON, but "No JSON object could be decoded" -

i have short json-code in "utf-8 without bom"-encoded file:

{ "paths": ["a:\\path\\to\\dir"], "anotherpath": "os.path.join(os.path.dirname( __file__ ), '..')" }

i ensured validity different online json validators. next python code...

jsonfile = "working\\path\\to\\myprogram.conf" open(jsonfile) conf: confcontent = json.load(conf) # dostuff...

... receive error:

no json object decoded

i know path correct, because read content @ different place. ideas wrong?

the problem don't have file encoded utf-8 without bom.

you can generate file string encoded way follows:

u='''{ "paths": ["a:\\path\\to\\dir"], "anotherpath": "os.path.join(os.path.dirname( __file__ ), '..')" }''' s=u.encode('utf-8') open('test.json', 'wb') f: f.write(s)

(whether 'b' necessary or not depends on whether you're on python 2 or 3, , whether you're on windows or unix. if it's not necessary, it's harmless.)

now, if run code against file, works.

but can compare test.json file working\\path\\to\\myprogram.conf file , see difference is. (most non-windows systems come command-line tools hexdump; on windows may have little fancier spot differences.)

python json

No comments:

Post a Comment