You can use the fact that Object is an HasMap and extract the key at runtime. You can then write the FromJSON instance as follows -
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Aeson
import qualified Data.Text as T
import qualified Data.HashMap.Lazy as HashMap
data TagResult = TagResult { name :: String
, numberOfDevicesTagged :: Int
} deriving (Show, Eq)
newtype TagResultList = TagResultList { tags :: [TagResult] } deriving Show
instance ToJSON TagResult where
toJSON (TagResult tag ntag) =
object [ T.pack tag .= ntag ]
instance ToJSON TagResultList where
toJSON (TagResultList tags) =
object [ "tagresults" .= toJSON tags ]
instance FromJSON TagResult where
parseJSON (Object v) =
let (k, _) = head (HashMap.toList v)
in TagResult (T.unpack k) <$> v .: k
parseJSON _ = fail "Invalid JSON type"
instance FromJSON TagResultList where
parseJSON (Object v) =
TagResultList <$> v .: "tagresults"
main :: IO ()
main = do
let tag1 = TagResult "tag1" 1
tag2 = TagResult "tag2" 7
taglist = TagResultList [tag1, tag2]
let encoded = encode taglist
decoded = decode encoded :: Maybe TagResultList
print decoded
The above program should print the tag result list.
Just (TagResultList {tags = [TagResult {name = "tag1", numberOfDevicesTagged = 1},TagResult {name = "tag2", numberOfDevicesTagged = 7}]})