2
votes

I am trying to write a JSON parser with Aeson.

The JSON I'm working with

The way I am calling the JSON in my code:

testReq :: Request
testReq = parseRequest_ "https://api.openweathermap.org/data/2.5/onecall?lat=41.63526&lon=-70.92701&exclude=minutely&appid=93120a85abf28f8fb1cdae14ffd7435d&units=metric"

First I define my custom type

type Celsius       = Double
type HPA           = Int --Hectopascal Pressure Unit
type Percent       = Int
type Meter         = Int
type MeterPerSec   = Double
type CompassDegree = Int

data WeatherObj =
  WeatherObj
    { time :: UTCTime
    , temp :: Celsius
    , feels_like :: Celsius
    , pressure :: HPA
    , humidity :: Percent
    , visibility :: Meter
    , wind_speed :: MeterPerSec
    , wind_deg :: CompassDegree
    }
  deriving (Eq, Show, Generic)

Next I write my FromJSON instance, which I know works because If I run parseCurrentWeather testReq I get back WeatherObj {time = 2020-07-19 16:54:43 UTC, temp = 25.51, feels_like = 29.49, pressure = 1012, humidity = 83, visibility = 10000, wind_speed = 1.34, wind_deg = 247} Which is perfect!

instance FromJSON WeatherObj where
  parseJSON = withObject "weatherObj" $ \obj -> do
    timeOffset  <- obj .: "timezone_offset"
    currentO    <- obj .: "current"
    dt          <- currentO .: "dt"
    temp        <- currentO .: "temp"
    feels_like  <- currentO .: "feels_like"
    pressure    <- currentO .: "pressure"
    humidity    <- currentO .: "humidity"
    visibility  <- currentO .: "visibility"
    wind_speed  <- currentO .: "wind_speed"
    wind_deg    <- currentO .: "wind_deg"
    pure $ WeatherObj (makeLocalTime dt timeOffset)
                       temp feels_like
                       pressure humidity
                       visibility wind_speed
                       wind_deg

parseCurrentWeather :: Request -> IO WeatherObj
parseCurrentWeather req = do
  current <- fetchCurrentWeather req
  pure $ getResponseBody current

Now I need to figure out how to parse the hourly weather which should give me back 48 objects. This code works as when I run parseHourly testReq I get back a long string of JSON with no exceptions. This JSON definitely matches the JSON from the link. I am looking great up to this point.

fetchHourly :: Request -> IO (Response HourlyWeathers) --Could also be IO (Response Object)
fetchHourly = httpJSON 

data HourlyWeathers =
  HourlyWeathers
    { getHours :: [Object] }
    deriving (Eq, Show, Generic)

instance FromJSON HourlyWeathers where
  parseJSON = withObject "hourlyWeather" $ \obj -> do
    allHours  <- obj .: "hourly"
    pure $ HourlyWeathers allHours
           
parseHourly :: Request -> IO HourlyWeathers
parseHourly req = do
  hours <- fetchHourly req
  pure $ getResponseBody hours

Now we are at the problematic code. I would like to map objToWeatherObj onto the list of objects that I generate with parseHourly. The problem that I cannot seem to overcome is that when I run parseHourlyObjects I get back a list of all Nothings.

parseHourlyObjects :: Request -> IO [Maybe WeatherObj]
parseHourlyObjects req = do 
  hourly <- fetchHourly req
  let x = getHours $ getResponseBody hourly
      y = fmap objToWeatherObj x 
  pure y

objToWeatherObj :: Object -> Maybe WeatherObj
objToWeatherObj = (decode . encode)

I have been able to write a ToJSON instance for WeatherObj but that turned out to be irrelevant because I need to parse a generic Object into a WeatherObj. I believe that the function I need here is decode, though I could be wrong.

1

1 Answers

4
votes

Given:

data WeatherObj =
  WeatherObj
    { time :: UTCTime
    , temp :: Celsius
    , feels_like :: Celsius
    , pressure :: HPA
    , humidity :: Percent
    , visibility :: Meter
    , wind_speed :: MeterPerSec
    , wind_deg :: CompassDegree
    }
  deriving (Eq, Show, Generic, FromJSON)

Note that it is now deriving FromJSON as well.

You can:

decode "{\"time\":\"...\",...}" :: Maybe WeatherObj

And get a Maybe WeatherObj. By writing your own instance of FromJSON, I think you may have made your life a bit more challenging than needed.