Skip to content Skip to sidebar Skip to footer

Python Json Dumps Puts Object Into Object On Row_to_json Return

I am running a Postgres query in python which returns me an array of JSON objects. db = SQLDB('postgres://postgres:*****@localhost:5433/postgres', migrate=False) q = ('SELE

Solution 1:

That is what happen if you dump the whole result set. With the t table:

createtable t (a int, b text);
insertinto t (a, b) values (1,'x'), (2,'y');

Using Psycopg2:

query = "select row_to_json(t) from t"
cursor.execute(query)
rs = cursor.fetchall()

# dump the whole result set
print json.dumps(rs)
print

# dump each column:
for r in rs:
    print json.dumps(r[0])
con.close()

Output:

[[{"a": 1, "b": "x"}], [{"a": 2, "b": "y"}]]

{"a": 1, "b": "x"}
{"a": 2, "b": "y"}

Post a Comment for "Python Json Dumps Puts Object Into Object On Row_to_json Return"