I would like to extract a slice of a JSON array using lenses, and get a Value
back. More specifically, I am trying to do the following:
$ import qualified Data.ByteString.Lazy as LBS
$ import Data.Aeson.Lens
$ import Control.Lens
$ let e = "{\"headers\":[[\"Host\",\"localhost:9090\"],[\"Accept-Encoding\",\"gzip\"]]}" :: LBS.ByteString
$ e ^? key "headers" . nth 0 . _Array . sliced 0 2
$ Just [String "Host",String "localhost:9090"]
It works but I would like the result to be a Value
and not a List
. How can I "reconstruct" a JSON array as part of the lens expression?
_Array
is a Prism
. This means you can invert it to get re _Array :: AsValue t => Getter (Vector Value) t
.
e ^? key "headers" . nth 0 . _Array . sliced 0 2 . re _Array
You can also use over
(or its alias (%~)
):
e ^? key "headers" . nth 0 . to (over _Array (slice 0 2))
e ^? key "headers" . nth 0 . to (_Array %~ slice 0 2)