0

I am trying to convert a JSON String into an ADT

This is my ADT:

data UserList = UserList
  { userListUsers :: [UserId] }

This is my FromJSON instance for UserList:

instance FromJSON UserList where
  parseJSON (Object o) = UserList
    <$> ((o .: "relationships") >>= (.: "users") >>= (mapM (.: "id")))

And finally this is my JSON String:

{
  "relationships": { 
    "users": [
      { "type": "User","id": "8" }
    ]
  }
}

My Yesod server is giving 400 Bad Request, without any further help, I think I may not be converting the users array correctly

0

2 Answers 2

3

Update

Your parser is fine as you can test with the code below. Your implementation is the same as the one I wrote with do-notation.

Original Answer

This should work:

{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson
import Data.Stringable
import Control.Monad

type UserId = String

data UserList = UserList
  { userListUsers :: [UserId] }
  deriving (Show)

instance FromJSON UserList where
  parseJSON (Object o) = 
    do  r <- o .: "relationships"
        u <- r .: "users"
        idents <- forM u $ \x -> x .: "id"
        return $ UserList idents

test = do
  contents <- readFile "in"
  let e = eitherDecode (toLazyByteString contents) :: Either String UserList
  print e
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, changing my code to do-notation some way helped me to realise what I was missing (fromPathPiece).
1

I forgot to use fromPathPiece, I changed to do-notation to make it prettier:

instance FromJSON UserList where
  parseJSON (Object o) = do  
    r <- o .: "relationships"
    u <- r .: "users"
    ids <- forM u $ \x -> do
      id <- x .: "id"
      return $ fromJust . fromPathPiece $ id
    return $ UserList ids

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.