Search code examples
jsonhaskellaesonhaskell-lens

Transforming a Haskell JSON Lens expression back to JSON


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?


Solution

  • 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)])