Skip to content

Instantly share code, notes, and snippets.

@hlian
Last active January 25, 2016 04:19
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 hlian/1d294a0e87736d5aaf26 to your computer and use it in GitHub Desktop.
Save hlian/1d294a0e87736d5aaf26 to your computer and use it in GitHub Desktop.
use JSON files as if they were Haskell values / live life maximally with simple language semantics
{
"hello": "world",
"this": {
"can": {
"be": "nested"
}
}
}
#!/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")
Just (Object (fromList [("hello",String "world"),("this",Object (fromList [("can",Object (fromList [("be",String "nested")]))]))]))
Just (String "world")
Just (String "nested")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment