Haskell: Parsing JSON data into a Map or a list of tuples?

haskell, json

Solution

You can use `aeson`. See Decoding a mixed-type object in its documentation's tutorial:

>>> import qualified Data.ByteString.Lazy.Char8 as BS
>>> :m +Data.Aeson
>>> let foo = BS.pack "{\"Name\" : \"Stitch\", \"Age\" : 3, \"Friend\": \"Lilo\"}"
>>> decode foo :: Maybe Object
Just fromList [("Friend",String "Lilo"),("Name",String "Stitch"),("Age",Number 3.0)]

An `Object` is just a `HashMap` from `Text` keys to `Value` values, the `Value` type being a sum type representation of JS values.

Problem

Is there a way to automatically convert JSON data into Data.Map or just a list of tuples? Say, if I have: ``` {Name : "Stitch", Age : 3, Friend: "Lilo"} ``` I'd like it to be converted into: ``` fromList [("Name","Stitch"), ("Age",3), ("Friend","Lilo")] ``` .. without defining a Stitch data type. I am happy to parse integers into strings in the resulting map. I can just read them into integers later.

Original source