{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} import Data.Aeson as AE import Data.Aeson.Types as AET import Data.ByteString.Lazy as BL import Data.ByteString.Lazy.Char8 as BL8 import Data.HashMap.Strict as HM import Prelude as P -- "id":7, -- "key":"gam", -- "title":"Gambia", -- "title2":null, -- "code":"GAM", -- "synonyms":null, -- "country_id":43, -- "city_id":null, -- "club":"f", -- "since":null, -- "address":null, -- "web":null, -- "assoc_id":null, -- "national":"f", -- "created_at":"2016-10-16 20:00:51.702726", -- "updated_at":"2016-10-16 20:00:51.702726" data Team = Team { _id :: Value, country :: Value } deriving (Show) instance ToJSON Team where toJSON Team{..} = object [ "i" .= _id, "c" .= country ] instance FromJSON Team where parseJSON = withObject "team" $ \o -> do _id <- o .: "id" country <- o .: "title" return Team{..} reduce :: [Team] -> Value -> [Team] reduce acc x = case (parseEither parseJSON x :: Either String Team) of (Left s) -> error s (Right v) -> v : acc parseTeams :: Either String [Value] -> [Team] parseTeams (Left x) = error x parseTeams (Right xs) = P.foldl reduce [] xs parseResult :: [Team] -> [(String, Value)] parseResult = P.foldl (\acc x -> (extractId x, country x) : acc) [] where extractId = BL8.unpack . encode . _id main :: IO () main = do src <- BL.readFile "./data/teams.json" let teams = parseTeams (AE.eitherDecode src :: Either String [Value]) let encoded = encode $ HM.fromList $ parseResult teams BL8.putStrLn encoded BL8.putStrLn "Writing games.json" BL8.writeFile "teams.json" encoded