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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:10:14+00:00 2026-06-14T03:10:14+00:00

I have a JSON request in the style of {command:get,params:{something:something else}} and this code

  • 0

I have a JSON request in the style of

{"command":"get","params":{"something":"something else"}}

and this code snippet from the Yesod book

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai (Response, responseLBS, Application, requestBody)
import Network.HTTP.Types (status200, status400)
import Network.Wai.Handler.Warp (run)
import Data.Aeson.Parser (json)
import Data.Conduit.Attoparsec (sinkParser)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (Value(..), encode, object, (.=))
import Control.Exception (SomeException)
import Data.ByteString (ByteString)
import Data.Conduit (ResourceT, ($$))
import Control.Exception.Lifted (handle)

main :: IO ()
main = run 3000 app

app :: Application
app req = handle invalidJson $ do
    value <- requestBody req $$ sinkParser json
    newValue <- liftIO $ modValue value
    return $ responseLBS
        status200
        [("Content-Type", "application/json")]
        $ encode newValue

invalidJson :: SomeException -> ResourceT IO Response
invalidJson ex = return $ responseLBS
    status400
    [("Content-Type", "application/json")]
    $ encode $ object
        [ ("message" .= show ex)
        ]

-- Application-specific logic would go here.
modValue :: Value -> IO Value
modValue (Object o) 
    | -- key "command" corresponds to value "get"
    | otherwise = fail "Invalid command"

But I can’t wrap my head around how I would destructure the generated Value data structure. How do I go about getting the values of keys etc. I realise I could parse to an explicitly defined data structure, but that would bring other kinds of problems to my use case.

In modValue I’ve put a comment where I can’t figure out what to put. I tried treating it as a Map, since that’s how it is implemented inside Aeson, but that obviously doesn’t type check.

EDIT:

Adding Data.HashMap to imports and using the line

    | M.lookup "command" o == Just "get" = return $ object [("result" .= (String "YAY"))]

gives the following error message.

main.hs:39:26:
Couldn't match expected type `M.Map k0 a0'
            with actual type `aeson-0.6.0.2:Data.Aeson.Types.Internal.Object'
In the second argument of `M.lookup', namely `o'
In the first argument of `(==)', namely `M.lookup "command" o'
In the expression: M.lookup "command" o == Just "get"

EDIT2:

On a sudden hunch, I tracked down an error message I got earlier involving “unordered-containers”. This is the package that Aeson uses. But I realised that I also had the package hashmap installed, which is imported as Data.HashMap. The hashmaps from unordered-containers are imported as Data.HashMap.Strict or Lazy!

Changing the line import qualified Data.HashMap as M to import qualified Data.HashMap.Strict as M fixed it anyway. Now the given answer works!

  • 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-14T03:10:15+00:00Added an answer on June 14, 2026 at 3:10 am

    Since an aeson JSON object is a Hashmap, you can use the Hasmap interface, in this case lookup.

    import qualified Data.HashMap.Strict as M
    
    M.lookup "command" o == Just "get"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an jQuery JSON request, that loads some JSON from another server (ex.
I have JSON like this: { something: http://something.com } and HTML like this: <a
I have the folliwng JSON request code on an ASP.NET MVC web application: var
I have a request(POST or GET), having one variable data (coming from Xcode) now
I have a JSON object returned from server. It looks like this : {1:{id:1,name:autos},
I have an NSMutablearray, populated by dictionaries [from a JSON request] i need to
I don't have any idea how to post a JSON RPC request using Obj-C.
I have a php function which returns JSON in response to a ajax request
I have the following result from geoPlugin http://www.geoplugin.net/json.gp?ip=64.90.182.55 : geoPlugin({ geoplugin_request:64.90.182.55, geoplugin_status:200, geoplugin_city:New York,
I have JSON text that looks like this: { ok: true, totalPages: 256, arReports:

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.