postgresqlhaskellpostgresql-simple

How to retrieve JSON jsonb value with postgresql-simple?


I have a column (jsonExample) in the postgresql database with type jsonb.

selectCALogs :: IO [(Int, Object)]
selectCALogs = do
  con <- connection
  query_ con "select \"clusterId\", \"jsonExample\" from cluster"

This gives an error of:

    • No instance for (Database.PostgreSQL.Simple.FromField.FromField
                         (unordered-containers-0.2.10.0:Data.HashMap.Base.HashMap
                            Text Value))
        arising from a use of ‘query_’
    • In a stmt of a 'do' block:
        query_ con "select \"clusterId\", \"clusterCALogs\" 
from cluster"
      In the expression:
        do con <- connection
           query_ con "select \"clusterId\", \"clusterCALogs\"from cluster"
      In an equation for ‘selectCALogs’:
          selectCALogs
            = do con <- connection
                 query_ con "select \"clusterId\", 
\"clusterCALogs\" from cluster"
   |
80 |   query_ con "select \"clusterId\", \"clusterCALogs\" 
from cluster"
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

How can I have it return a JSON object - using aeson or something else?


Solution

  • Looking at the FromField instances here (http://hackage.haskell.org/package/postgresql-simple-0.6.2/docs/Database-PostgreSQL-Simple-FromField.html#t:FromField) I realized it should be a Value and not a Object.

    Hence:

    selectCALogs :: IO [(Int, Value)]
    selectCALogs = do
      con <- connection
      query_ con "select \"clusterId\", \"jsonExample\" from cluster"