Skip to content

@23Skidoo /JSON.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
More fun with Aeson
{-# LANGUAGE OverloadedStrings #-}
-- See
-- http://stackoverflow.com/questions/7800272/trying-to-parse-recursive-json-am-i-on-the-right-track/7800464
import Control.Applicative
import Control.Monad
import Data.Aeson
import Data.Attoparsec
import qualified Data.ByteString as B
import Data.Map as M
import Data.Text
import qualified Data.Vector as V
data Outer = Outer {
oName :: Text,
oProducts :: M.Map Text Inner
} deriving Show
data Inner = Inner {
iQA :: Text,
iVM :: Text,
iAvailable :: V.Vector Text
} deriving Show
instance FromJSON Outer where
parseJSON (Object o) = Outer <$> o .: "name" <*> o .: "products"
parseJSON _ = mzero
instance FromJSON Inner where
parseJSON (Object o) = Inner <$> o .: "qa" <*> o .: "vm" <*> o .: "available"
parseJSON _ = mzero
parseAll :: B.ByteString -> Outer
parseAll s = case (parse (fromJSON <$> json) s) of
Done _ (Success e) -> e
_ -> error "Should not happen!"
main :: IO ()
main = do p <- parseAll <$> B.readFile "json.txt"
print p
{
"name": "versions",
"products":
{
"IM":
{
"qa":"5.0.01.004",
"vm":"5.0.01.003",
"available":
[
"3.1.02.003",
"3.9.0",
"3.9.1",
"3.9.2",
"4.0.01.004",
"4.0.01.005",
"4.1.00.001",
"4.1.00.002",
"4.1.01.001",
"4.1.01.002",
"4.1.01.003",
"4.2.00.001",
"4.2.00.002",
"4.2.01.001",
"4.2.01.002",
"4.2.01.003",
"4.2.01.004",
"4.3.00.001",
"4.3.00.002",
"4.3.00.003",
"4.3.00.004",
"4.3.00.005",
"4.3.01.001",
"5.0.00.001",
"5.0.00.002",
"5.0.00.003",
"5.0.00.004",
"5.0.00.005",
"5.0.00.006",
"5.0.01.001",
"5.0.01.002",
"5.0.01.003",
"5.0.01.004"
]
},
"IB":
{
"qa":"5.0.01.004",
"vm":"5.0.01.003",
"available":
[
"3.1.02.003",
"3.9.0",
"3.9.1",
"3.9.2",
"4.0.01.004",
"4.0.01.005",
"4.1.00.001",
"4.1.00.002",
"4.1.01.001",
"4.1.01.002",
"4.1.01.003",
"4.2.00.001",
"4.2.00.002",
"4.2.01.001",
"4.2.01.002",
"4.2.01.003",
"4.2.01.004",
"4.3.00.001",
"4.3.00.002",
"4.3.00.003",
"4.3.00.004",
"4.3.00.005",
"4.3.01.001",
"5.0.00.001",
"5.0.00.002",
"5.0.00.003",
"5.0.00.004",
"5.0.00.005",
"5.0.00.006",
"5.0.01.001",
"5.0.01.002",
"5.0.01.003",
"5.0.01.004"
]
}
}
}
$ ./Main json.txt
Outer {oName = "versions", oProducts = fromList [("IB",Inner {iQA = "5.0.01.004", iVM = "5.0.01.003", iAvailable = fromList ["3.1.02.003","3.9.0","3.9.1","3.9.2","4.0.01.004","4.0.01.005","4.1.00.001","4.1.00.002","4.1.01.001","4.1.01.002","4.1.01.003","4.2.00.001","4.2.00.002","4.2.01.001","4.2.01.002","4.2.01.003","4.2.01.004","4.3.00.001","4.3.00.002","4.3.00.003","4.3.00.004","4.3.00.005","4.3.01.001","5.0.00.001","5.0.00.002","5.0.00.003","5.0.00.004","5.0.00.005","5.0.00.006","5.0.01.001","5.0.01.002","5.0.01.003","5.0.01.004"] :: Data.Vector.Vector}),("IM",Inner {iQA = "5.0.01.004", iVM = "5.0.01.003", iAvailable = fromList ["3.1.02.003","3.9.0","3.9.1","3.9.2","4.0.01.004","4.0.01.005","4.1.00.001","4.1.00.002","4.1.01.001","4.1.01.002","4.1.01.003","4.2.00.001","4.2.00.002","4.2.01.001","4.2.01.002","4.2.01.003","4.2.01.004","4.3.00.001","4.3.00.002","4.3.00.003","4.3.00.004","4.3.00.005","4.3.01.001","5.0.00.001","5.0.00.002","5.0.00.003","5.0.00.004","5.0.00.005","5.0.00.006","5.0.01.001","5.0.01.002","5.0.01.003","5.0.01.004"] :: Data.Vector.Vector})]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.