Reputation: 893
How can I suffix the following Aeson Lens expression
>>> "{\"a\": 4, \"b\": 7}" & members . _Number *~ 10
"{\"a\":40,\"b\":70}"
so that the result is a Value
(with an Object
constructor) and not a String
?
Upvotes: 3
Views: 136
Reputation: 2869
You can use decode
from aeson to parse your string and then use lenses as before:
ghci> (decode "{\"a\": 4, \"b\": 7}" :: Maybe Value ) & _Just . members . _Number *~ 10
Just (Object fromList [("a",Number 40.0),("b",Number 70.0)])
Upvotes: 2
Reputation: 30113
You can use the _Value
prism to convert to Maybe Value
, then proceed from there. The flipped fmap
operator <&>
from the lens library provides nice syntax for cases like this:
"{\"a\": 4, \"b\": 7}"^? _Value <&> members . _Number *~ 10
-- Just (Object fromList [("a",Number 40.0),("b",Number 70.0)])
Upvotes: 3