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

  • Home
  • SEARCH
  • 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 7062763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:37:59+00:00 2026-05-28T04:37:59+00:00

I want to parse a JSON object and create a JSONEvent with the given

  • 0

I want to parse a JSON object and create a JSONEvent with the given name and args

I’m using Aeson, and right now I’m stucked on converting "args":[{"a": "b"}] to a [(String, String)].

Thank’s in advance!

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative
import Data.Aeson

data JSONEvent = JSONEvent [(String, String)] (Maybe String) deriving Show

instance FromJSON JSONEvent where
  parseJSON j = do
    o <- parseJSON j
    name <- o .:? "name"
    args <- o .:? "args" .!= []
    return $ JSONEvent args name

let decodedEvent = decode "{\"name\":\"edwald\",\"args\":[{\"a\": \"b\"}]}" :: Maybe JSONEvent
  • 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-28T04:38:00+00:00Added an answer on May 28, 2026 at 4:38 am

    Here’s a bit more elaborate example based on ehird’s example. Note that the explicit typing on calls to parseJSON is unnecessary but I find them useful for documentation and debugging purposes. Also I’m not sure what you intended, but with args with multiple values I simply concatenate all the args together like so:

    *Main> decodedEvent2
    Just (JSONEvent [("a","b"),("c","d")] (Just "edwald"))
    *Main> decodedEvent3
    Just (JSONEvent [("a","b"),("c","d")] (Just "edwald"))
    

    Here’s the code:

    {-# LANGUAGE OverloadedStrings #-}
    
    import Control.Applicative
    import qualified Data.Text as T
    import qualified Data.Foldable as F
    import qualified Data.HashMap.Lazy as HM
    import qualified Data.Vector as V
    import Data.Aeson
    
    import qualified Data.Attoparsec as P
    import Data.Aeson.Types (Parser)
    import qualified Data.Aeson.Types as DAT
    import qualified Data.String as S
    
    data JSONEvent = JSONEvent [(String, String)] (Maybe String) deriving Show
    
    instance FromJSON JSONEvent where
      parseJSON = parseJSONEvent
    
    decodedEvent = decode "{\"name\":\"edwald\",\"args\":[{\"a\": \"b\"}]}" :: Maybe JSONEvent
    decodedEvent2 = decode "{\"name\":\"edwald\",\"args\":[{\"a\": \"b\"}, {\"c\": \"d\"}]}" :: Maybe JSONEvent
    decodedEvent3 = decode "{\"name\":\"edwald\",\"args\":[{\"a\": \"b\", \"c\": \"d\"}]}" :: Maybe JSONEvent
    
    emptyAesonArray :: Value
    emptyAesonArray = Array $ V.fromList []
    
    parseJSONEvent :: Value -> Parser JSONEvent
    parseJSONEvent v =
      case v of
        Object o -> do
          name <- o .:? "name"
          argsJSON <- o .:? "args" .!= emptyAesonArray
          case argsJSON of
            Array m -> do
              parsedList <- V.toList <$> V.mapM (parseJSON :: Value -> Parser (HM.HashMap T.Text Value)) m
              let parsedCatList = concatMap HM.toList parsedList
              args <- mapM (\(key, value) -> (,) <$> (return (T.unpack key)) <*> (parseJSON :: Value -> Parser String) value) parsedCatList
              return $ JSONEvent args name
            _ -> fail ((show argsJSON) ++ " is not an Array.")
        _ -> fail ((show v) ++ " is not an Object.")
    
    -- Useful for debugging aeson parsers
    decodeWith :: (Value -> Parser b) -> String -> Either String b
    decodeWith p s = do
      value <- P.eitherResult $ (P.parse json . S.fromString) s
      DAT.parseEither p value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, this is a sketch of the JSON object I want to parse
I want to create a data structure that will be parse as a JSON
I'm using the Facebook php-sdk to get the users name and friends, right now
I want to deserialize a JSON object (using GSON, because I already use it
I want to parse JSON arrays and using gson. Firstly, I can log JSON
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I'm trying to parse some JSON using the JSon.Net library. The documentation seems a
I have an XML file which I want to convert into JSON file using
I am trying to parse a json string straight into a managed object. The
I have the following JSON object as a String: [{Add1:101,Description:null,ID:1,Name:Bundesverfassung,Short:BV},{Add1:220,Description:null,ID:2,Name:Obligationenrecht,Short:OR},{Add1:210,Description:null,ID:3,Name:Schweizerisches Zivilgesetzbuch,Short:ZGB},{Add1:311_0,Description:null,ID:4,Name:Schweizerisches Strafgesetzbuch,Short:null}] Now i

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.