3

I'm trying to upload and parse json files using django. Everything works great up until the moment I need to parse the json. Then I get this error:

No JSON object could be decoded: line 1 column 0 (char 0)

Here's my code. (I'm following the instructions here, and overwriting the handle_uploaded_file method.)

def handle_uploaded_file(f, collection):
#  assert False, [f.name, f.size, f.read()[:50]]
  t = f.read()
  for j in serializers.deserialize("json", t):
    add_item_to_database(j)

The weird thing is that when I uncomment the "assert" line, I get this:

[u'myfile.json', 59478, '']

So it looks like my file is getting uploaded with the right size (I've verified this on the server), but the read command seems to be failing entirely.

Any ideas?

6
  • What's type(f) and t come out to? My suspicion is .read() works, but something isn't valid JSON. Commented Nov 15, 2011 at 20:00
  • From: assert False, [f.name, f.size, f.read()[:50], type(f), type(t), len(t)] I get, [u'wikileaks_comments.json', 59478, '', <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>, <type 'str'>,0] Commented Nov 15, 2011 at 20:01
  • Cool. And what's in the string? Commented Nov 15, 2011 at 20:02
  • Re: not valid JSON -- That was my first guess too. But the file was created using simplejson, and I've run it through a validator. I'm pretty confident the json isn't the problem. Commented Nov 15, 2011 at 20:04
  • Nothing -- it's a string of length zero. Commented Nov 15, 2011 at 20:04

1 Answer 1

2

I've seen this before. Your file has length, but reading it doesn't. I'm wondering if it's been read previously... try this:

f.seek(0)
f.read()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.