Home > Blockchain >  Haskell (Aeson): Idiomatic way to filter specific elements question
Haskell (Aeson): Idiomatic way to filter specific elements question

Time:11-09

I've a Jsonl file which looks like this:

{"mood": "Good", "when": "2022-10-09 Sun 11:51"} 

I'm using Aeson, and Relude as an alternative prelude and I I'm trying to get that when field, to be parsed as a UTCTime as part of a bigger data strcuture. But as UTCTime consists of date-time, not date, day, time.

And I think my parser is failing due to this. So I'm trying to figure out how to filter that out in an idiomatic way, so I can parse it as UTCTime or at the very least narrow down the problem.

I don't know if the idiomatic way there is to take the byteString and turn it into something else for instance text so I can use filter on it, then back to a byteString for for feeding into a fromJSON function.

Or if there is a better way for instance using something in the lens package, which I know of vaguely. I've not looked at it heavily.

So asking here what the idiomatic way would be to get rid of that "Sun"? Any suggestions thanks.

CodePudding user response:

You can simply use parseTimeM from time to parse this for you:

ghci> parseTimeM False defaultTimeLocale "%Y-%m-%d %a %H:%M" "2022-10-09 Sun 11:51" :: Maybe UTCTime
Just 2022-10-09 11:51:00 UTC

Most of the formats are documented in formatTime

  • Related