Returning actual Data in Json

  • Building out some hardcoded users

  • need to supply User before the data

  • don’t forget the = sign

  • We could manually build the encode and decode to/from JSON, but Generics, lets you automatically do things based on the data type

    • adding {-# LANGUAGE DeriveGeneric #-} and import GHC.Generics

    • don’t forget to add instance

    • instance ToJSON User
      instance FromJSON User

Now we are able to see the output of having the generic encoder

Ok, two modules loaded.
*Main Lib> import Data.Aeson (encode)
*Main Lib Data.Aeson> encode bob
"{\"userName\":\"Bobby Boy\",\"userId\":1}"
  • And now with that it looks like Scotty has a function json for use to return a json payload. sweet
    get "/users" $ do
        json allUsers