Created
September 1, 2015 15:57
-
-
Save r-wheeler/9e3e1e4e9d92d2c4b1e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--{"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