Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Parse Json tweets using Aeson (Tushar Jarhad)
   2. Re:  Parse Json tweets using Aeson (Michael Alan Dorman)
   3. Re:  Parse Json tweets using Aeson (Michael Alan Dorman)


----------------------------------------------------------------------

Message: 1
Date: Fri, 19 Sep 2014 03:53:50 +0530
From: Tushar Jarhad <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Parse Json tweets using Aeson
Message-ID:
        <CAH5gX+49=N2B39th0=o1_ytffvbyc+czhrwces_hgiult2m...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

import Data.Aeson
import GHC.Generics
import qualified Data.ByteString.Lazy as B

data  Tweet  =  Tweet {
                        id_str :: String,
                        text   :: String
                      }deriving Generic


instance FromJSON Tweet


main = do
  input <- B.readFile "1.json"
  let mm = decode input :: Maybe Tweet
  case mm of
    Nothing -> print "error parsing JSON"
    Just m -> (putStrLn.greet) m

greet m = (show.id_str) m


This program is fine if 1.json file has 1 tweet
if 1.json file has multiple tweets then it fails
so how we can parse multiple tweets
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140919/fd82005d/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 19 Sep 2014 04:40:55 -0400
From: Michael Alan Dorman <[email protected]>
To: Tushar Jarhad <[email protected]>
Cc: Haskell Beginners <[email protected]>
Subject: Re: [Haskell-beginners] Parse Json tweets using Aeson
Message-ID: <[email protected]>
Content-Type: text/plain

Tushar Jarhad <[email protected]> writes:
> let mm = decode input :: Maybe Tweet

You have told the program you expect one tweet---anything else is, by
definition, an error. The program takes you at your word.

So if you want it to parse multiple tweets, tell it that you expect a
*list* of tweets.

> let mm = decode input :: Maybe [Tweet]

Or are you saying that your file simply has multiple tweets appended one
after another?  If so, that file does not represent a valid JSON
structure.

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.

Mike.


------------------------------

Message: 3
Date: Fri, 19 Sep 2014 04:51:24 -0400
From: Michael Alan Dorman <[email protected]>
To: Tushar Jarhad <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parse Json tweets using Aeson
Message-ID: <[email protected]>
Content-Type: text/plain

Michael Alan Dorman <[email protected]> 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.


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 75, Issue 13
*****************************************

Reply via email to