Skip to content

Instantly share code, notes, and snippets.

@safiire
Created January 17, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safiire/21be9a91942ef563a97f4421d710d597 to your computer and use it in GitHub Desktop.
Save safiire/21be9a91942ef563a97f4421d710d597 to your computer and use it in GitHub Desktop.
Parsing JSON in Haskell
{-# 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