How to parse nested JSON with aeson

Viewed 2466

I am trying to parse JSON of the following form using aeson

{"field":{"name":"..."}}

or

{"tag":{"name":"..."}}

or

{"line":{"number":"..."}}

to construct the following data type

data Rule = Line Integer
          | Field L.ByteString
          | Tag L.ByteString

Unfortunately, I face two problems that I've not found solutions to, namely:

  1. How do I parse nested JSON? Looking at the implementation of (.:), it uses lookup to extract a specific key's value. I'm hesitant to do something like this as it seems to be relying too much on the specifics of how aeson implements things. Am I wrong in thinking this is an issue?

  2. How do I use the correct data constructor based on which key is present in the JSON? All my efforts with <|> have led me nowhere.

I would post the code I've written thus far, but I haven't even gotten to the point where I have anything worth posting.

2 Answers
Related