3

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?

z1naOK9nu8iY5A
  • 903
  • 7
  • 22

2 Answers2

3

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)])
András Kovács
  • 29,931
  • 3
  • 53
  • 99
2

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)]) 
Markus1189
  • 2,829
  • 1
  • 23
  • 32