Skip to content

convert Record object to json #263

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

Closed
python-programmer opened this issue Feb 15, 2018 · 3 comments
Closed

convert Record object to json #263

python-programmer opened this issue Feb 15, 2018 · 3 comments

Comments

@python-programmer
Copy link

hi, i have a table like this:

Product
    Id
    Name
    Price

And i get a product with this:

async with db.acquire() as connection:
    product = await connection.fetchrow('''
        SELECT id, name, price FROM product WHERE id = 1
    ''')

i want the below result:

{
    'id': 1,
    'name': 'asyncpg',
    'price': 3
}

i want to get a json result from query, and Question is: How do it?

@elprans
Copy link
Member

elprans commented Feb 15, 2018

Cast it to a dict: product = dict(connection.fetchrow(...

@mecampbellsoup
Copy link

@elprans I am serializing the JSON in Postgres via json_build_object and friends - will this generate another JSON serialization of the data received by the time it reaches asyncpg and I do dict(conn.fetchrow...)?

@Havanero
Copy link

cast it to dict is really bad solution what if I want to interact with variables as dot notation without passing it to Pydantic or any other dataclass? We loose the built in hybrid_property technique by doing so. For example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants