Lesson 40. Working with JSON data by using Aeson

 

After reading lesson 40, you’ll be able to

  • Transform Haskell types into JSON
  • Read JSON into a Haskell type
  • Use the DeriveGeneric extension to implement needed classes
  • Write you own instances of ToJSON and FromJSON

In this lesson, you’ll work with JavaScript Object Notation (JSON) data, one of the most popular ways to store and transmit data. The JSON format originates in simple JavaScript objects and is heavily used in transmitting data through HTTP APIs. Because the format is so simple, it has seen widespread adoption outside the web, frequently being used as a method of storing data and for tasks such as creating configuration files. Figure 40.1 shows an example JSON object used with the Google Analytics API.

Figure 40.1. An example of JSON data from the Google Analytics API

In the previous lesson, you ended up downloading a JSON file containing information on data sets available through the NOAA Climate Data API. In this lesson, you’re going to build a simple command-line application that opens that JSON file and prints out the data sources in the file. Before you get there, you’ll learn how to work with JSON. You’ll create types that you can turn into JSON as well as create types representing JSON that you’ll read in.

40.1. Getting set up

40.2. Using the Aeson library

40.3. Making your data types instances of FromJSON and ToJSON

40.4. Putting it all together: reading your NOAA data

Summary

sitemap