Created
January 17, 2017 13:57
-
-
Save safiire/21be9a91942ef563a97f4421d710d597 to your computer and use it in GitHub Desktop.
Parsing JSON in Haskell
This file contains 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
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-} | |
import Data.Text (Text) | |
import Data.Aeson | |
import GHC.Generics | |
import qualified Data.ByteString.Lazy as B | |
data Person = | |
Person { first :: !Text | |
, last :: !Text | |
, age :: Int | |
} deriving (Show, Generic) | |
instance FromJSON Person | |
instance ToJSON Person | |
main :: IO () | |
main = do | |
d <-(eitherDecode <$> B.readFile "people.json") :: IO (Either String [Person]) | |
case d of | |
Left err -> putStrLn err | |
Right ps -> print $ map (\x -> first x) ps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment