-
Notifications
You must be signed in to change notification settings - Fork 419
Automatically convert Records to json #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You need to encode it as json. Either do it explicitly via I'm using tornado's
Hope this helps, cheers! |
With a play dump i get How well does this approach perform for huge results? Thanks EDIT Ok, I managed to overcome the issue, though it's not optimal. It would be an interesting feature to directly have JSON result instead of a list of records which have to be serialized into JSON |
I would recommend server-side JSON serialization using |
But I don't have JSON values in my database. I use JSON as a mean to transfer data from my api to the frontend. That's why I would like to directly return the result of the query, limiting the overhead of converting the list of records into a list of dictionaries and then returning it. |
The JSON support is not limited to JSON values, you can serialize arbitrary data to JSON in Postgres. |
But then also the asyncpg result is a json? Or will it still be incapsulated into |
If you use |
For a query shaped like the following: SELECT json_build_object(
'id', u.id,
'first_name', u.first_name,
'last_name', u.last_name,
'email', u.email,
'date_joined', u.date_joined
) AS user,
json_build_object(
'id', o.id,
'name', o.name,
'credit_card_id', o.credit_card_id
) AS organization
FROM iam_user u
LEFT JOIN organization o ON u.email = o.owner_id
WHERE u.email = :email; ... using However using I'd really love to avoid an extra serialization in Python since my query returns serialized JSON - any suggestions @elprans? cc @lsabi in case you were doing something similar (unclear from your original question though). |
I've already checked the following issues, but none of them work for my case
#17
#263
Basically, I'm fetching several records for an API that should return a JSON response. The code I'm using is the following
async with db_pool.acquire() as conn: async with conn.transaction(): res = await conn.fetch(query, param) return res
Even by adding
res = [dict(r.items()) for r in res]
the response is not correct for javascript that has to fetch from the client side the JSON data.How can I correctly transform the resulting list of records into a valid JSON? I haven't found anything in the docs.
No, I'm not using sqlalchemy or similars. They add too much overhead.
Thanks.
The text was updated successfully, but these errors were encountered: