Retrieving values ​​from JSON using an ezon lens

I just read the tutorial at https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms and I successfully wrote a request in json bytestring. However, I do not get the result value that I want.

I would like to do something line by line

if (j^? key "some key" == Just "Google") then ... else ... 

But (j ^? Key "some key") has type "Maybe Value".

This should be a fairly common template that would be surprised if there were no utility function to turn the value into text. Any ideas?

+6
source share
2 answers

There is! _String Prism is of type Prism' Value Text , i.e. Attempts to cross the Value branch containing a Text . So can you do j ^? key "some key" . _String == Just "Google" j ^? key "some key" . _String == Just "Google" j ^? key "some key" . _String == Just "Google" .

+3
source

My fu lens is pretty limited, but it looks like you need the _String method:

 if (j^? key "some key" >>= (^? _String)) == Just "Google" 

Or you can convert the right side to Value :

 if (j^? key "some key" == Just (String "Google")) 
+3
source

Source: https://habr.com/ru/post/953879/


All Articles