0

I know this might be a level 0 question, but I'm stuck here

I'm using SQL Alchemy to get the sql result.

with db.connect() as conn:
    data = conn.execute('''select * from tables''')
    json_data = json.dumps([(dict(row.items())) for row in data])
    conn.close()
    return json_data

The result for the above snippet is

[{"id": 1, "name": "Ashton", "age": 20, "nationality": "US"}, {"id": 2, "name": "Kevin", "age": 20, "nationality": "US"}]

But how do I get the response with a name?

{"citizens":[{"id": 1, "name": "Ashton", "age": 20, "nationality": "US"}, {"id": 2, "name": "Kevin", "age": 20, "nationality": "US"}]}

1 Answer 1

1

You could do the following...

with db.connect() as conn:
    data = conn.execute('''select * from tables''')
    json_data = json.dumps({"citizens": [(dict(row.items())) for row in res]})
    return json_data

Note: you also don't need call conn.close(). This is called by default at the end of the with db.connect() context manager.

https://docs.sqlalchemy.org/en/14/core/connections.html#basic-usage

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.