I'm trying to parse some JSON.
Given a very simple [Object]
how can I get the Value
under a string key?
Attempt one (guessing):
d . key "test"
Errors with:
• Couldn't match expected type ‘Parser [Object]’
with actual type ‘(Value -> f0 Value) -> c0’
Attempt two (from reading https://hackage.haskell.org/package/aeson-lens-0.5.0.0/docs/Data-Aeson-Lens.html):
d ^. key "test"
Errors with:
• Couldn't match expected type ‘Parser [Object]’
with actual type ‘Value’
Full code:
{-# Language OverloadedStrings #-}
module JobManagerApi where
import Network.Wreq
import Job
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens (_String, key)
import Data.Aeson.Types
import Data.ByteString.Lazy
-- parseResponse :: ByteString -> Either String String
parseResponse z = do
result <- eitherDecode z
flip parseEither result (\obj -> do
d <- obj .: "data"
-- k <- d . key "test"
return (d :: [Object])
)
apiPendingJobs :: IO [Job]
apiPendingJobs = do
r <- get "http://localhost:3000/user_job_queue"
let x = (r ^. responseBody)
print $ parseResponse x
pure []
The relavent bit being:
flip parseEither result (\obj -> do
d <- obj .: "data"
-- k <- d . key
return (d :: [Object])
)
d .: "test"
https://hackage.haskell.org/package/aeson-1.4.6.0/docs/Data-Aeson.html#v:.:
Retrieve the value associated with the given key of an Object. The result is empty if the key is not present or the value cannot be converted to the desired type.