I would like to parse the following JSON using Aeson in Haskell:
{
"foo": {
"name": "name 1",
"location": "location 1"
},
"bar": {
"name": "name 2",
"location": "location 2"
}
}
The keys name
and location
are known, but foo
and bar
are unknown.
I would like to load the JSON data as a list of the following data type ([Entry]
):
data Entry = Entry
{ id :: String -- "foo" or "bar" etc.
, name :: String -- "name 1" or "name 2" etc.
, location :: String -- "location 1" or "location 2" etc.
} deriving Show
My first try looks as follows (it does not work, yet):
instance FromJSON Entry where
parseJSON (Object o) = do
map (\(id, (name, location)) -> Entry id name location) o
parseJSON o
How would I parse the JSON correctly?