[Haskell-beginners] Parse Json tweets using Aeson

Michael Alan Dorman mdorman at ironicdesign.com
Fri Sep 19 08:51:24 UTC 2014


Michael Alan Dorman <mdorman at ironicdesign.com> writes:
> I feel sure there's a way to run the parse incrementally---taking a
> complete data structure at a time---but I don't know it offhand.

Since I felt like I *should* know this offhand, I decided to figure it
out.  It's very easy---Aeson provides an Attoparsec parser, so you can,
in fact, do parsing-with-leftovers with virtually zero effort:

    import Data.Aeson
    import Data.Attoparsec.ByteString
    import Data.ByteString

    var :: ByteString
    var = "{\"Some\" : \"Text\"}Leftover"

    main = print $ parse json var

This produces a value representing the simple Object, as well as
leftovers---so you could, presumably, do a fold to pull out as many JSON
items as can be parsed, even if the overall file isn't a valid JSON
structure in itself.

Mike.


More information about the Beginners mailing list