[Haskell-beginners] How to parse JSON into existential type

David McBride toad3k at gmail.com
Tue Aug 29 12:56:06 UTC 2017


Rather than having an existential, which is difficult to work with at
the best of times, how about making RespItem into something concrete?
You could make your own data type to represent strings and arrays and
objects, etc, but you could also just use Value from aeson, since it
already exists and already has a FromJSON instance.

data Resp = Resp {
  rHeadings :: [T.Text],
  rRow :: [Data.Aeson.Value]
}

I couldn't get deriving Generic to work, but I've personally not had
too much trouble writing aeson instances manually.

instance FromJSON Resp where
  parseJSON = withObject "Resp" $ \v -> Resp <$> v .: "headings" <*> v .: "rows"

There is also an aeson-lens package that can make parsing json easy,
if you are into lenses.


On Tue, Aug 29, 2017 at 8:36 AM, Baa <aquagnu at gmail.com> wrote:
> Hello List!
>
> In some of my JSONs I have part "list of any values", for example:
>
>   [1, "hi", "false", 2.71]
>
> To parse it I create type like:
>
>   data RespItem = forall a. FromJSON a => RespItem {
>     riItem :: [a] }
>
> ,-------------------------------------------------------------------
> | btw, I try `(FromJSON a, Show a, Generic a) => ...` also but don't
> | see any difference, so I'm not sure what is right constraints list.
> `-------------------------------------------------------------------
>
> And GHC does not allow to derive automatically with "deriving" Show
> and Generic instances for existential types, as I see. So I write:
>
>   instance Generic RespItem
>   instance Show RespItem
>
> Also this type `RespItem` in part of another one:
>
>   data Resp = Resp {
>     rHeadings :: [T.Text]
>     , rRow    :: [RespItem]
>     } deriving (Generic, Show)
>
> Interesting is that now auto-`deriving` works! OK, but I need to
> to instantiate `FromJSON` for RespItem because next step must be
> `FromJSON` for Resp. So I'm trying:
>
>   instance FromJSON RespItem where
>     parseJSON ??? = ???
>
> and here I get different errors, and absolutely don't understand how to
> parse it, I get "ambiguous type error..." and so on (which is right,
> sure).
>
>
> ===
> Best regards, Paul
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list