Search code examples
pythonjsonpostgresqlcompressionbytea

How to read column in postgres DB of data type BYTEA and encoding of compressed json using PYTHON


I have a column in postgres DB of data type bytea and encoding is of compressed_json. How can I read the values of this column and get a proper json back in Python?


Solution

  • But the method gzip.compress and decompress is not showing. Maybe its a version issue with my gzip module. Anyways, I was able to decompress using below way :

    1. Convert and store the column as byte array :-

         payload = bytearray(row[0]) 
      
    2. For decompressing, used below code snippet :

         fileobj = cStringIO.StringIO(payload)
         gzf = gzip.GzipFile('dummy-name', 'rb', 9, fileobj)
         decomprJson = gzf.read()