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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:55:26+00:00 2026-05-24T00:55:26+00:00

I am trying to parse JSON of the following form using aeson {field:{name:…}} or

  • 0

I am trying to parse JSON of the following form using aeson

{"field":{"name":"..."}}

or

{"tag":{"name":"..."}}

or

{"line":{"number":"..."}}

to construct the following data type

data Rule = Line Integer
          | Field L.ByteString
          | Tag L.ByteString

Unfortunately, I face two problems that I’ve not found solutions to, namely:

  1. How do I parse nested JSON? Looking at the implementation of (.:), it uses lookup to extract a specific key’s value. I’m hesitant to do something like this as it seems to be relying too much on the specifics of how aeson implements things. Am I wrong in thinking this is an issue?

  2. How do I use the correct data constructor based on which key is present in the JSON? All my efforts with <|> have led me nowhere.

I would post the code I’ve written thus far, but I haven’t even gotten to the point where I have anything worth posting.

  • 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-24T00:55:28+00:00Added an answer on May 24, 2026 at 12:55 am

    How about the following?

    {-# LANGUAGE OverloadedStrings #-}
    
    import Control.Applicative
    import           Data.Aeson
    import           Data.Aeson.Types
    import qualified Data.ByteString      as B
    import qualified Data.ByteString.Lazy as L
    import qualified Data.Map             as M
    
    data Rule = Line Integer
              | Field L.ByteString
              | Tag L.ByteString
              deriving Show
    
    instance FromJSON Rule where
      parseJSON j = do
        o <- parseJSON j -- takes care of JSON type check
        case M.toList (o :: Object) of
          [("field", Object o')] -> Field <$> o' .: "name"
          [("tag",   Object o')] -> Tag   <$> o' .: "name"
          [("line",  Object o')] -> Line  <$> o' .: "number"
          _                      -> fail "Rule: unexpected format"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse the following JSON string remotely: [{name:Object1,data:[1,2]},{name:Object2,data:[3,4]}] I am doing
I'm trying to parse this JSON data: into R, using the following lines of
I am trying to parse the json values from the following stream: {id: tag:search.xxxxx.com,2005:xxxxxxxxxxx}
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I am trying to send JSON data from a form using the XMLHttpRequest object.
I am trying to parse the following JSON data, but it seems that it
Trying to learn how to use JSON and parse the json data.i am using
I'm trying to parse the following json line in jquery: [{ pk: 19, model:
Trying to parse the following json using gson in my android app but givien
I'm trying to parse this JSOn with multiple entries of node tag: { nodes:

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.