I have an object to parse that looks a bit like this :
{
\"data\":
[
{
\"virtio0\": \"some text\",
\"virtio1\": \"blah\",
\"ide2\": \"s
If as you said there are only 9 virtio and 2 ide, one simple and perhaps not so elegent way to do is to use the asum function from Data.Foldable (which is generalised choice from various parsing libraries)
import Control.Applicative
instance FromJSON VM where
parseJSON = withObject "VM" $ \o -> do
cores <- o .: "cores"
mem <- o .: "mem"
disk <- optional $ asum [
o .: "virtio0",
o .: "virtio1",
o .: "virtio2",
return VM{..}
I haven't tried the code yet. For further reference, see this link for a comprehensive guide of haskell JSON parsing with the Aeson library.