Parsing json with a lens eson

An attempt to develop an expanding Edward Kmett lens and an ezon lens package. Here is a warm up

s = "{ \"somekey\" : [ { \"deeperkey\" : 1} , {\"deeperkey\": 2, \"otherkey\":3}]}

Desired Result:

[(1, Nothing), (2, Just 3)]

I can advance a bit with

import Control.Lens.Aeson
import Control.Lens
import Control.Monad
import qualified Data.Vector as V

λ> s ^. key "somekey" . _Array . to V.toList >>= (^.. key "deeperkey" . _Number)
[1,2]

But I still do not understand how to work with combinators is difficult enough to get the right answer. I'm most likely involved in the “key“ otherkey ”snippet. Any ideas?

+4
source share
1 answer

try it

s ^.. key "somekey" . _Array . traverse 
                    . to (\o -> ( o ^?! key "deeperkey" . _Number
                                , o ^?  key "otherkey"  . _Number
                                )
                         )

(^..), traverse Traversal Array "somekey". key "deeperkey" , , , (^?!) (^?) Maybe .

+3

All Articles