How to convert a Postgres query result to JSON or String in Python

Viewed 717

I have a Postgres database and I want to send the result of a query to the Azure Service bus. The output of the query is:

[(226530, 5, 'GDGDGh', '326', 'Logical Name', 553713665, 537985025, 136, 0, 301, datetime.datetime(2021, 3, 8, 19, 46, 26, 536000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), datetime.datetime(2021, 3, 8, 19, 46, 25, 749508, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), datetime.datetime(2021, 3, 8, 19, 46, 25, 795195, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 2, {'error': {}, 'MSG_TYPE_TAG': '301', 'ATTACHED_DEVICE_SERIAL_NUMBER_TAG': '537985025', 'HDGDGDGD_TILTDATA_LOG_TAG': {'date_time': '2021-0 ... (531 characters truncated) ... 19:46:26.537', 'sequence_number': 420745, 'inclination_x_date_time': '2021-03-08 19:46:26.539', 'inclination_y_date_time': '2021-03-08 19:46:26.540'}}, 0, 420745)]

If I send this output to the service bus queue, I received the below error:

TypeError: ServiceBusMessage body must be a string, bytes, or None.  Got instead: <class 'list'>

I tried to convert this output to JSON or String with to_string() and to_json() , I received the below error:

AttributeError: 'list' object has no attribute 'to_string'
AttributeError: 'list' object has no attribute 'to_json'

Also, I used json.dumps() to convert, I received this error:

TypeError: Object of type LegacyRow is not JSON serializable

Are there any solutions to convert this output and sed it to Azure service bus?

My code is:

import os
import psycopg2
import pandas as pd
import datetime
import sqlalchemy
import json
from azure.servicebus import ServiceBusClient, ServiceBusMessage


connstr = "Endpoint=s*****"
queue_name = '****'


def get_engine(database='***_rms', username='user**', password='pass**', host='***', port=5432):
    engine_string = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}"
    engine = sqlalchemy.create_engine(engine_string)
    return engine


def read_from_db(table_name, date_time):
    acceleration_array = []
    engine = get_engine()
    connection = engine.connect()
    metadata = sqlalchemy.MetaData()
    pinconnector_attacheddevicelogdata = sqlalchemy.Table(
        table_name, metadata, autoload=True, autoload_with=engine)
    query = sqlalchemy.select([pinconnector_attacheddevicelogdata]).where(pinconnector_attacheddevicelogdata.columns.capture_time > date_time)\
        .order_by(pinconnector_attacheddevicelogdata.columns.capture_time).limit(1)
    ResultProxy = connection.execute(query)
    ResultSet = ResultProxy.fetchall()
    engine.dispose()
    return ResultSet


def get_date_time(date_time_string):
    [date, time] = date_time_string.split(" ")
    [year, month, day] = date.split("-")
    [hour, minute, second] = time.split(":")
    [second, microsecond] = second.split(".")
    [year, month, day, hour, minute, second, microsecond] = list(
        map(lambda x: int(x), [year, month, day, hour, minute, second, microsecond]))
    return datetime.datetime(year, month, day, hour, minute, second, microsecond, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None))


def write_date_time(date_time, address="date_time.txt"):
    file1 = open(address, "w+")
    file1.write((str(date_time)).split("+")[0])
    file1.close()


def read_date_time(address="date_time.txt"):
    file1 = open(address, "r")
    date_time = file1.readline()
    date_time = get_date_time(date_time)
    file1.close()
    return date_time


def get_query_latest_date_time(db_query, date_time):
    for item in db_query:
        if item["capture_time"] > date_time:
            date_time = item["capture_time"]
    return date_time


date_time = read_date_time()
db_query = read_from_db("pinconnector_attacheddevicelogdata", date_time)
write_date_time(db_query[-1]["capture_time"])
print(db_query)
#data_send = db_query.to_string()
#data_send = json.dumps(db_query)


with ServiceBusClient.from_connection_string(connstr) as client:
    with client.get_queue_sender(queue_name) as sender:
        single_message = ServiceBusMessage(data_send)
        sender.send_messages(single_message)
0 Answers
Related