liveBook · Manning

parseJSON

As explained in Learn Haskell by Example

parseJSON is the FromJSON class method that converts a JSON value into a Haskell value or reports a parsing failure.

parseJSON is the FromJSON method that turns a JSON Value into a Haskell value through a Parser, reporting failure when the input does not match. section 16.1.1

A typical implementation:

  • Uses withObject to require a JSON object and produce clearer type errors. section 16.1.1

Parsing a TodoEntry

instance J.FromJSON TodoEntry where parseJSON = todoEntryParser Valid JSON produces a TodoEntry; omitting required checked produces the error key "checked" not found.

How Learn Haskell by Example covers it

How deep it goes
Explains the Value-to-Parser type, helper functions, object fields, errors, and instance wiring, but not parser internals.
Examples
Uses a TodoEntry parser with optional identifiers and ranks, required text and checked fields, and successful or failing JSON inputs.
What it assumes
Assumes JSON values, Haskell data types, and type classes; teaches parser construction through a concrete TodoEntry API model.
What you can do after
Apply it via Data.Aeson to define FromJSON instances for custom Haskell data types.
How it connects
Connects FromJSON, Parser, Value, JSON decoding, withObject, and object-field operators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.