Working with predefined lenses
In this recipe, we will work with predefined lenses. We will use a library lens-aeson
for dealing with JSON data. The aeson library is a popular library for parsing and writing JSON instances for the user-defined data. Often, we have to deal with JSON, and parse it on the fly to extract the desired data.
Getting ready
In this recipe, we will use the generic JSON parser provided by aeson, and then use lens-aeson to dig through the JSON to extract the data.
How to do it...
- Create a new project,
working-with-aeson-lens
, with asimple
stack template.
stack new working-with-aeson-lens simple
- Add a dependency on the lens library in the
build-depends
sub-section of theexecutable
section. Also add dependency onaeson
andlens-aeson
libraries. Also addbytestring
and text, as aeson usesbytestring
for parsing andtext
for string values. Addvector
, as we need to work with arrays in JSON:
executable working-with-aeson-lens hs-source-dirs: src main-is: ...