Created
April 18, 2018 07:07
-
-
Save naoto-ogawa/fa3f026593098fa8e6e1434b8d8eea81 to your computer and use it in GitHub Desktop.
JSON access by Lens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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