Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7171923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:30:38+00:00 2026-05-28T15:30:38+00:00

Haskell beginner here. I want to pass a type parameter with JSON, and have

  • 0

Haskell beginner here. I want to pass a “type” parameter with JSON, and have it parse into a native Haskell type using read. The following works:

data CreatureType = Bot | Player deriving (Generic, Typeable, Show, Read)
data Creature = Creature CreatureType deriving (Typeable, Show)

instance FromJSON Creature where
    parseJSON (A.Object v) = Creature <$> read <$> (v .: "type")  
    parseJSON _          = mzero

I want to change it to work with readMay, so that it doesn’t crash if read fails. I keep getting stuck. parseJSON needs to return mzero if readMay returns Nothing.

Main Question: How can I get this function to use readMay to return mzero on a failure with read?

Here’s what I’ve tried:

instance FromJSON Creature where
    parseJSON (A.Object v) = do
        let t = (v .: "type") :: Parser String
            mt = readMay <$> t :: Parser (Maybe CreatureType)
        -- ?? 
        -- guard -- I can't use this because it wants a Bool, and I only have Parser Bool
        -- Creature <$> read <$> mt
    parseJSON _          = mzero

mt is Parser (Maybe CreatureType). How can I change what I return if it is Nothing? I can’t use pattern matching, because Data.Aeson doesn’t seem to export a value constructor for Parser. I can’t seem to find any methods that will give me the value inside a parser, so, is there a generic haskell method that does it? I’ve looked at Control.Applicative and can’t find anything. Here are the docs for Data.Aeson. http://hackage.haskell.org/packages/archive/aeson/0.5.0.0/doc/html/Data-Aeson.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:30:39+00:00Added an answer on May 28, 2026 at 3:30 pm

    Doesn’t binding the value in the do-block work?

    maybeCreature <- mt
    case maybeCreature of
      Nothing -> mzero
      Just cr -> return (Creature cr)
    

    or similar?

    instance FromJSON Creature where
        parseJSON (A.Object v) = do
            let t = (v .: "type") :: Parser String
                mt = readMay <$> t :: Parser (Maybe CreatureType)
            maybeCreature <- mt
            -- variant 1, guard
            guard (isJust maybeCreature)
            return (Creature $ fromJust maybeCreature)
            -- variant 2, fromMaybe
            fromMaybe mzero (return . Creature) maybeCreature
            -- variant 3, case, see above
        parseJSON _          = mzero
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am an absolute beginner to haskell but I want to dive into the
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
I am a beginner interested in Haskell, and I have been trying to implement
First, I'm a Haskell beginner. I'm planning integrating Haskell into C for realtime game.
I have a beginner question about dates and String in Haskell. I need to
I'm a Haskell beginner and have to define a series of known statuses that
Beginner Haskell learner here. I've solved Project Euler Problem 1, sum of multiples of
Haskell's Text.JSON library uses an abstract data type called Result, it's basically their form
I'm playing around with beginner Haskell, and I wanted to write an average function.
I am a beginner in Haskell so please bear with me. (Just started learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.