Note that there are some explanatory texts on larger screens.

plurals
  1. POparsing complicated jsons with Aeson
    text
    copied!<p>I'm trying to parse a call to an API into a haskell record type using the Aeson Library</p> <p>I'm using wikipedia pages, and parsing them to the title and a list of links. A sample would be this,</p> <pre><code>{"query":{"pages":{"6278041":{"pageid":6278041,"ns":0,"title":"Lass","links":[{"ns":0,"title":"Acronym"},{"ns":0,"title":"Dead Like Me"},{"ns":0,"title":"Donna Lass"},{"ns":0,"title":"George Lass"},{"ns":0,"title":"Girl"},{"ns":0,"title":"Lassana Diarra"},{"ns":0,"title":"Lightning Lass"},{"ns":0,"title":"Real Madrid"},{"ns":0,"title":"Shadow Lass"},{"ns":0,"title":"Solway Lass"},{"ns":0,"title":"Szymon Lass"},{"ns":0,"title":"The Bonnie Lass o' Fyvie"},{"ns":0,"title":"The Tullaghmurray Lass"},{"ns":0,"title":"Woman"},{"ns":12,"title":"Help:Disambiguation"}]}}}} </code></pre> <p>and I would like to parse it to the title and a list of links in a data type like this.</p> <pre><code>data WikiPage = WikiPage { title :: String, links :: String } </code></pre> <p>What code I currently have is this,</p> <pre><code>instance FromJSON WikiPage where parseJSON j = do o &lt;- parseJSON j let id = head $ o .: "query" .: "pages" let name = o .: "query" .: "pages" .: id .: "title" let links = mapM (.: "title") (o .: "query".: "pages" .: id .: "links") return $ WikiPage name links </code></pre> <p>I'm getting the error, </p> <pre><code>Couldn't match expected type `Data.Text.Internal.Text' with actual type `[Char]' In the second argument of `(.:)', namely `"title"' </code></pre> <p>I don't really get whats going on, I feel like there must be a problem with how I'm mapping over the links but I'm not sure exactly what has to be done. I also don't get how I'm supposed to use id in the second query string as it's a parser (I'm sure I need to use applicative in here somewhere but I'm not sure how.) I haven't found any examples that decompose more complicated jsons like this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload