Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created April 18, 2018 07:07
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 naoto-ogawa/fa3f026593098fa8e6e1434b8d8eea81 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/fa3f026593098fa8e6e1434b8d8eea81 to your computer and use it in GitHub Desktop.
JSON access by Lens
{-# LANGUAGE OverloadedStrings #-}
module Test06 where
import Prelude hiding (readFile, lookup)
import Control.Monad.IO.Class
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import qualified Data.ByteString as B
main :: IO ()
main = do
val <- B.readFile "test/Data/test01.json"
print val
print $ val ^? key "lng" . nonNull
print $ val ^? key "lng" . _String
print $ val ^? key "resources" . _Object
print $ val ^? key "resources" . key "en" . key "translation" . _Object
print $ val ^? key "resources" . key "en" . key "translation" . key "look" . key "deep" . _Value
print $ val ^? (foldl (.) _Value $ map key ["resources" , "en", "translation"])
{-
test/Data/test01.json
{
"lng": "en",
"debug": true,
"resources": {
"en": {
"translation" : {
"key": "value of key",
"look": {
"deep": "value of look deep"
}
}
}
}
}
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment