8

I have a simple code to fetch users from db using sqlalchemy and return them as json. My problem is how to format the output to get something like this:

{"results": [{"id":1, "username":"john"},{"id":2,"username":"doe"}]} 

my code outputs an error which I cant seem to fix being a newbie in python:

d = [] for user in Users.query.all(): v = {} for columnName in Users.__table__.columns.keys(): v[columnName] = getattr( user, columnName ) d.append( v ) return jsonify( d ) 

The code says:

ValueError: dictionary update sequence element #0 has length 11; 2 is required 

Thanks.

7
  • 1
    In which line do you get this error? Commented May 30, 2011 at 19:20
  • 4
    Could you paste the whole stacktrace? Commented May 30, 2011 at 19:20
  • 1
    Not the reason for your error, but if you want to have that data structure, you need to change the return line to: return jsonify( {"results": d} ) Commented May 30, 2011 at 19:27
  • 1
    Full stack trace at: paste.pocoo.org/show/397870 Full code at: paste.pocoo.org/show/397871 Commented May 30, 2011 at 19:32
  • 1
    Also, you're using a property that's supposed to be private (__table__), there are other ways to get that metadata, using the API plus some helpers. Commented May 30, 2011 at 19:43

4 Answers 4

10

I solved this error by simply saying

return jsonify( results = d ) 

instead of

return jsonify( d ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. The reason behind this is that the jsonify in flask will not encode a raw array (in square brackets) at the top level. I don't know exactly why, but that is considered unsafe.
7

Ah, now that your code has been pasted, I can see that the fundamental problem is indeed coming from jsonify. The below workaround should be satisfactory.

>>> import json >>> json.dumps({"results": [{"id":1, "username":"john"},{"id":2,"username":"doe"}]}) '{"results": [{"username": "john", "id": 1}, {"username": "doe", "id": 2}]}' 

Replace jsonify with json.dumps, and let me know if that doesn't fix the problem.

But if you'd prefer to use flask.jsonify, then you should take a look at the flask documentation. The argument to jsonify should be the same as the argument to any dict constructor -- i.e. a dict or an iterable of tuples. So that's the problem.

1 Comment

BTW, simplejson lets you register additional encoders with it so you can extend it with more complex objects.
0

You're calling dict() with *args, which is going to expand args to positional parameters. That is a typical cause of that error you are seeing. Leave that out.

1 Comment

He is somewhere, or is being called with some parameters he provided.
0

The error is what you'd expect when creating a dict incorrectly - see Python dictionary creation error

Edited: See answer from senderle that identifies the root cause...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.