user1328021
user1328021

Reputation: 9840

I'm getting error No JSON object could be decoded but with a valid JSON file

I'm using Django-Smuggler to upload JSON data to my database.

When I load a JSON file I'm getting the error: No JSON object could be decoded

I used online JSON validators to make sure the data is valid and it is. I got that data by doing a data dump.

Anyone know why this is happening?

This is where the exception is being raised (starred):

try:
    for format, stream in data:
        objects = serializers.deserialize(format, stream)
        for obj in objects:
            model = obj.object.__class__
            if router.allow_syncdb(using, model):
                models.add(model)
                counter += 1
                obj.save(using=using)
    if counter > 0:
        sequence_sql = connection.ops.sequence_reset_sql(style, models)
        if sequence_sql:
            for line in sequence_sql:
                cursor.execute(line)
**except Exception, e:**
    transaction.rollback(using=using)
    transaction.leave_transaction_management(using=using)
    raise e

Also here is my general format of JSON file:

[
  {
"pk": 1, 
"model": "auth.message", 
"fields": {
  "message": "Successfully uploaded a new avatar.", 
  "user": 1
}
      }, 
  {
"pk": 2, 
"model": "auth.message", 
"fields": {
  "message": "You have saved model 'mymodel'", 
  "user": 1
}
  }, 
      {
"pk": 3, 
"model": "auth.message", 
"fields": {
  "message": "You have saved model 'sdfsd'", 
  "user": 1
}
  }, 
  {
"pk": 4, 
"model": "auth.message", 
"fields": {
  "message": "Successfully uploaded a new avatar.", 
  "user": 1
}
  }, 
  {
"pk": 5, 
"model": "auth.message", 
"fields": {
  "message": "Successfully updated your avatar.", 
  "user": 1
}
  }
]

Upvotes: 2

Views: 10002

Answers (2)

Khoi
Khoi

Reputation: 4635

I'm a bit late to this but your JSON file might not be so valid even though it passed jsonlint. Check if it uses UTF-8 without BOM encoding.

Upvotes: 4

jhauberg
jhauberg

Reputation: 1430

You need to remove the very last ',' character in the JSON string.

Upvotes: 7

Related Questions