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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:19:26+00:00 2026-06-11T16:19:26+00:00

I have a complex nested json, which i’m trying to parse with Aeson and

  • 0

I have a complex nested json, which i’m trying to parse with Aeson and Attoparsec, into my custom types. Based on info from questions: Haskell, Aeson & JSON parsing into custom type, Aeson: How to convert Value into custom type? and some info from Internet.

When I’m using following code I’m getting “Nothing” Value from overlapped FromJSON instance, but code goes through each instance for sure, I’ve tested this by disabling some other insances. So the main question : how to test code in instances and see how data changes over execution in GHCi?

P.S: Tried to set breakpoints and “trace”, but they are worked only in main & parseCfg functions.

{-# LANGUAGE OverloadedStrings, FlexibleInstances #-}

-- high level data
data Cfg = Cfg { nm  :: CProperty,
             author :: CProperty,
             langs :: CValue,
             grops :: CListArr,
             projs :: CPropArr
           } deriving (Show)

...

instance FromJSON CProperty where
parseJSON _          = mzero
parseJSON (Object o) = CProperty <$> toCProperty o
  where
    toCProperty :: (HM.HashMap T.Text Value) -> J.Parser (T.Text, T.Text)
    toCProperty _  = error "unexpected property"
    toCProperty o' = do
      l <- return $ HM.toList o'
      k <- return $ fst $ head l
      v <- return $ snd $ head l
      v' <- parseJSON v
      return $ (k, v')

... lot's of different instances

-- |this instance is specific for different files
-- based on common functions to work with most of nested json code
instance FromJSON Cfg where
  parseJSON _          = mzero
  parseJSON (Object o) = do
    nm     <- (parseJSON :: Value -> J.Parser CProperty) =<< (o .: T.pack "Name")
    autor  <- (parseJSON :: Value -> J.Parser CValue)    =<< (o .: T.pack "Author")
    langs  <- (parseJSON :: Value -> J.Parser CProperty) =<< (o .: T.pack "Languages")
    groups <- (parseJSON :: Value -> J.Parser CListArr)  =<< (o .: T.pack "Groups")
    projs  <- (parseJSON :: Value -> J.Parser CPropArr)  =<< (o .: T.pack "Projects")
    return $ Cfg nm author langs groups projs
------------------------------------------------------------------------------------

main :: IO ()
main = do:
  s <- L.readFile "/home/config.json"
  -- print $ show s
  let cfg =  parseCfg s
  print $ show $ cfg

parseCfg :: L.ByteString -> Maybe Cfg
parseCfg s = decode s
  • 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-06-11T16:19:27+00:00Added an answer on June 11, 2026 at 4:19 pm

    The obvious problem is that in

    instance FromJSON CProperty where
        parseJSON _          = mzero
        parseJSON (Object o) = ...
    

    the first clause matches all input, so your instance returns mzero whatever the argument is. You should change the order of the clauses.

    When compiling with warnings, GHC would tell you of the overlapping patterns.

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

Sidebar

Related Questions

I am using WCF and REST, and I have complex types, which are working
I have javascript object which consists of complex array of structures with nested structures
I have a somewhat complex query with multiple (nested) sub-queries, which I want to
I'm trying to create a controller for a custom complex object but have some
If I have an object that is composed of nested complex types, e.g. a
Here is the situation. I have some ViewModels that contain nested complex model types.
I have a complex nested-query which is inside a join, is it possible to
We have a complex app that serves AJAX JSON streams (using ADO to grab
I have some complex regular expressions which I need to comment for readability and
I have a complex type defined which doesn't currently contain any minOccurs restrictions. When

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.