0
votes

I am trying to get JSON rows from postgres using "psycopg2". value of records is like [ [{....},{...},{...}] ]. To get the correct JSON result ie. [{....},{...},{...}] i have to go for records1. Not sure why this is happening.

import psycopg2
import sys
import json

conn_string = "'host='localhost' dbname='postgres' user='postgres' password='password123'" 
con=psycopg2.connect(conn_string)
cur = con.cursor()
cur.execute("select json_agg(art) from (select a.*, (select json_agg(b) from (select * from pfstklist where pfportfolioid = a.pfportfolioid ) as b) as pfstklist, (select json_agg(c) from (select * from pfmflist where pfportfolioid = a.pfportfolioid ) as c) as pfmflist from pfmaindetail as a) art")
records = cur.fetchall()
print(records)     #This gives result [[{....},{...},{...}]]
records1=records[0]    
print(records1)   #This gives expected result [{....},{...},{...}]
1
is it python?!! tag the language - Aryan Firouzian

1 Answers

-1
votes

cur.fetchall() gives you a list of records. You probably want cur.fetchone().