Skip to content

Instantly share code, notes, and snippets.

@r-wheeler
Created September 1, 2015 15:57
Show Gist options
  • Save r-wheeler/9e3e1e4e9d92d2c4b1e8 to your computer and use it in GitHub Desktop.
Save r-wheeler/9e3e1e4e9d92d2c4b1e8 to your computer and use it in GitHub Desktop.
--{"message": "test",
-- "results": {"k":"value"}
--}
-- Simple Nested JSON Parse with Aeson
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative ((<$>), (<*>))
import Control.Lens
import Control.Monad (mzero)
import Data.Aeson
import Data.ByteString.Lazy as LBS
import qualified Data.Map as M
import Data.Monoid ((<>))
import qualified Data.Text as T
import Network.Wreq
data Message = Message {
v1 :: !T.Text
, v2 :: !T.Text
} deriving (Show)
instance FromJSON Message where
parseJSON (Object o) =
Message <$> (o .: "message")
<*> ((o .: "results") >>= (.:"k"))
parseJSON _ = mzero
-- in Prelude
r <- BS.readFile "p.json"
let m = decode r :: Maybe Message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment