You can simply pattern match on the constructors of A.Value
in order to figure out what kind of JSON object you are dealing with:
import qualified Data.HashMap.Strict as M
import qualified Data.Vector as V
import qualified Data.Text as T
-- ... rest of the code more or less as before ...
navigate :: A.Value -> [String] -> BSL.ByteString
navigate value [] = A.encode value
navigate (A.Array vs) (x : xs) = navigate (vs V.! read x) xs
navigate (A.Object o) (x : xs) = navigate (o M.! T.pack x) xs
Note that the definition of A.Value
is as follows:
data Value
= Object !(HashMap Text Value)
| Array !(Vector Value)
| ... -- other constructors
The code for navigate
is thus making use of the lookup function (called !
in both cases) on vectors and hash maps. The function read
is used to interpret a command line argument as a number if needed (and will fail horribly if it isn't), whereas T.pack
is reinterpreting the string as a value of type Text
.