The following post is part of Spoke Proof. Tell your friends.
Using JSON files as if they were Haskell values
life life maximally with simple language semantics
JSONBaby.hs
#!/usr/bin/env stack
-- stack --resolver lts-4.2 --install-ghc runghc --package file-embed --package aeson --package lens --package lens-aeson
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module JSONBaby where
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import Data.FileEmbed
value :: Maybe Value
value = (decode . view lazy) $(embedFile "a.json")
main :: IO ()
main = do
print value
print (value ^? _Just . key "hello")
print (value ^? _Just . key "this" . key "can" . key "be")
a.json
{
"hello": "world",
"this": {
"can": {
"be": "nested"
}
}
}
output
Just (Object (fromList [("hello",String "world"),("this",Object (fromList [("can",Object (fromList [("be",String "nested")]))]))]))
Just (String "world")
Just (String "nested")