A postgres query returns json array and I am trying to dump this into json file.
query = f"""
SELECT array_to_json(array_agg(results))
FROM
(SELECT * from lookup_brands limit 1000) as results
"""
json_results = exec_query(self.conn_string, query)
print(json_results) # outputs <sqlalchemy.engine.cursor.LegacyCursorResult object at 0x7f30d64bd8b0>
print(type(json_results) # outputs <class 'sqlalchemy.engine.cursor.LegacyCursorResult'>
I tried writing json_results to json like:
with open(output_path, 'w') as outfile:
json.dump(json_results, outfile)
And I get this error
error Object of type LegacyCursorResult is not JSON serializable
which is expected.
How can I dump this json array result to the file?