So, I've hit something of a roadblock with parsing the following JSON with the Haskell Aeson library.
So say I have the following:
"packetX_Name": [
"container",
[
{
"field1": "value1",
"field2": "value2"
},
{
"field1": "value3",
"field2": "value4"
},
{
"field1": "value5",
"field2": "value6"
}
]
],
"packetY_Name": [
"container",
[
{
"field1": "value7",
"field2": "value8"
},
{
"field1": "value9",
"field2": "value10"
}
]
],
etc...
And I would ideally like to parse this using data types like this:
data ExtractedPacket = ExtractedPacket
{ packetName :: String
, packetFields :: [ExtractedPacketField]
} deriving (Show,Eq)
instance FromJSON ExtractedPacket where
parseJSON = blah
data ExtractedPacketField = ExtractedPacketField
{ field1 :: String
, field2 :: String
} deriving (Show,Eq)
instance FromJSON ExtractedPacketField where
parseJSON = blah
And get something like the following:
ExtractedPacket
"packetX_Name"
[ ExtractedPacketField "value1" "value2"
, ExtractedPacketField "value3" "value4"
, ExtractedPacketField "value5" "value6"
]
ExtractedPacket
"packetY_Name"
[ ExtractedPacketField "value7" "value8"
, ExtractedPacketField "value10" "value10"
]
This JSON example is describing network packets and each packet has a different name (such as "packetX_Name") that can't be parsed the same way "field1" or "field2" can be. It'll be different every time. Most of the Aeson examples out there are quite unhelpful when it comes to situations like this. I've noticed a function in the API docs called withArray that matches on a String, but I'm at a lose as to what to use for (Array -> Parser a)
The part I'm really stuck on is parsing the heterogeneous array that starts with a String "container" and then has an array with all the objects in it. Thus far, I've been indexing straight to the array of objects, but the type system started to become a real labyrinth and I found it really hard to approach this in a way that isn't ugly and hackish. On top of this, Aeson doesn't produce very helpful error messages.
Any ideas on how to approach this?