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 6690971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:41:52+00:00 2026-05-26T05:41:52+00:00

This problem is related to this question. Here is the data type I wish

  • 0

This problem is related to this question.

Here is the data type I wish to make from the JSON:

data ProdObject = MKSpair (Text, Text)
                | MKSLpair (Text, [Text])
                | MKSOpair (Text, ProdObject)
                | MKObject ProdObject
                | End
                deriving Show

Here is a sample of the data I am working with, plus a generalization of the whole.

Here’s my instance definition, which is causing an error. I’ve used this as a reference. I’m not sure if the error is telling me to fix my type, or that I’m way off. If the error really is straight-forward, I’d like some advice on how to fix my type,plus any advice on what else I may be doing wrong but haven’t noticed yet.

instance FromJSON ProdObject where
  parseJSON (Object o) = MKObject <$> parseJSON o
  parseJSON (String s, String t)  = MKSpair (s, t)
  parseJSON (String s, Object o)  = MKSOpair (s, MKObject <$> parseJSON o)
  parseJSON (String s, Array a) = MKSLpair (s, V.toList a)
  parseJSON (Done d) = End
  parseJSON _        = mzero

Here’s the error I have right now:

ghcifoo> :load test
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:23:52:
    Couldn't match expected type `Value'
                with actual type `Data.Map.Map Text Value'
    Expected type: Value
      Actual type: Object
    In the first argument of `parseJSON', namely `o'
    In the second argument of `(<$>)', namely `parseJSON o'
Failed, modules loaded: none.

Update: I’ve redone my data type, if I’m right I’ve got a phantom type. If I’m wrong, back to the drawing board

data ProdObject = MKSpair (Text, Text)
                | MKSLpair (Text, [Text])
                | MKSOpair (Text, ProdObject)
                | MKObject ProdObject (k,v)
                | End

Also I have reflected this change in my instance, though in an incomplete manner. I mention this just to ask if I am on the right track or not.

parseJSON (Object (k,v)) = MKObject ...

If I’m on the right track, I think I can either figure out the rest, or at least ask a specific question. Feedback anyone?

  • 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-26T05:41:53+00:00Added an answer on May 26, 2026 at 5:41 am

    In this equation:

    parseJSON (Object o) = MKObject <$> parseJSON o
    

    o has type Map Text Value, but parseJSON has type Value -> Parser a, so you obviously can’t apply parseJSON to o.

    Also, you have a type error here:

    parseJSON (String s, String t)  = MKSpair (s, t)
    

    The type of parseJSON is Value -> Parser a, but you are trying to match against (Value, Value).

    The same applies to this line:

    parseJSON (Done d) = End
    

    Done is not a constructor of type Value.


    I couldn’t understand why you need the ProdObject type to be recursive; here’s how I would solve this problem:

    data Outer = Outer {
      oName :: Text,
      oProducts :: M.Map Text Inner
    } deriving Show
    
    data Inner = Inner {
      iQA :: Text,
      iVM :: Text,
      iAvailable :: V.Vector Text
    } deriving Show
    
    instance FromJSON Outer where
        parseJSON (Object o) = Outer <$> o .: "name" <*> o .: "products"
        parseJSON _ = mzero
    
    instance FromJSON Inner where
      parseJSON (Object o) = Inner <$> o .: "qa" <*> o .: "vm" <*> o .: "available"
      parseJSON _ = mzero
    

    Full code listing can be found on Github.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to my problem in understanding rebase, branch and merge ,
I got this from php.net website. This is related to the problem I am
This question is related to one I asked previously, see here . As a
I have tried solving this problem by posting other related questions here that focused
This question is related to others I have asked on here, mainly regarding sorting
I am working on this problem and had previously asked related question. Implementation of
I have searched here, but haven't found any question related to this. I got
This seems to be a problem related to Safari only. I've tried 4 on
I'm having a memory problem related to UIImageView. After adding this view to my
some time ago I happend to resolve a PG related problem with this SO

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.