haskell - How to parse JSON with Aeson without declaring individual types -


(haskell newbie alert)

here's snippet of code i'm struggling with. basically, i'm taking json coming in websocket , want parse using aeson without defining individual data types each response.

import data.aeson import qualified network.websockets  ws  aria2websocketreceiver :: ws.connection -> io () aria2websocketreceiver conn =   msg <- ws.receivedata conn   let res = decode msg   let v = flip parsemaybe res $ \o ->                                       r <- o .: "result"                                       version <- r .: "version"                                       enabledfeatures <- r .: "enabledfeatures"                                       id_ <- r .: "id"                                       return $ "version=" ++ version   putstrln (show v)   aria2websocketreceiver conn 

here compilation errors i'm running into:

nightwatch/telegram.hs:244:13:     no instance (fromjson a0) arising use of ‘decode’     type variable ‘a0’ ambiguous     relevant bindings include       res :: maybe a0 (bound @ nightwatch/telegram.hs:244:7)     note: there several potential instances:       instance fromjson chat -- defined @ nightwatch/telegram.hs:90:10       instance fromjson message         -- defined @ nightwatch/telegram.hs:106:10       instance fromjson telegramresponse         -- defined @ nightwatch/telegram.hs:122:10       ...plus 2 others     in expression: decode msg     in equation ‘res’: res = decode msg     in expression:       { msg <- ws.receivedata conn;            let res = decode msg;            let v = flip parsemaybe res $ ...;            putstrln (show v);            .... }  nightwatch/telegram.hs:246:44:     couldn't match type ‘maybe a0’                    ‘unordered-containers-0.2.5.1:data.hashmap.base.hashmap                            text value’     expected type: object       actual type: maybe a0     relevant bindings include       o :: maybe a0 (bound @ nightwatch/telegram.hs:245:34)       res :: maybe a0 (bound @ nightwatch/telegram.hs:244:7)     in first argument of ‘(.:)’, namely ‘o’     in stmt of 'do' block: r <- o .: "result" 

i'm trying replicate "working ast" example given @ https://hackage.haskell.org/package/aeson-0.10.0.0/docs/data-aeson.html

thanks cale @ #haskell here's working code:

aria2websocketreceiver :: ws.connection -> io () aria2websocketreceiver conn =   msg <- ws.receivedata conn   let v = res <- decode msg              flip parsemaybe res $ \o ->                                          r <- o .: "result"                                          version <- r .: "version"                                          return $ "version=" ++ (version :: string)   putstrln (show v)   aria2websocketreceiver conn 

there 3 problems in earlier code:

  1. the type of decode msg maybe needs inside separate do block.
  2. because of {-# language overloadedstrings #-} compiler wasn't being able infer type version, hence (version :: string) hint.
  3. similarly, enabledfeatures , id_ being assigned not being used anywhere, causing more problem in type-inference.

Comments