FAQ Database Discussion Community


Expecting a type but actually getting a Maybe a0

json,haskell,types,aeson
I have a function which uses Aeson to get data from a json object within a file. The data and function are both defined below. data Data = Data { date :: String , temperature :: Int } deriving (Show, Read, Generic) data Temperatures = Temperatures { temperatures :: [Data]...

Composing optional Aeson parsers

haskell,monads,composition,parsec,aeson
Here is a part of JSON object which represents the user: { "image": { "url": "http://example.com" } } I need to parse it into a User type: data User = User { imgUrl :: Maybe Text } The naive solution: parseJSON (Object o) = User <$> getImgUrl o where getImgUrl...

Extract nested property inside Aeson object

haskell,aeson
How can I get a nested property using Data.Aeson? For example, when decoding an arbitrary JSON string using Value like this: decode "{\"foo\":{\"bar0\":\"foobar0\", \"bar1\":\"foobar1\"}}" :: Maybe Value I end up with this: Just (Object (fromList [("foo",Object (fromList [("bar1",String "foobar1"),("bar0",String "foobar0")]))])) Now, how can I write a function [String] -> Object...

Haskell ADTs with aeson

haskell,aeson
I've been fighting with a simple ADT, trying to get it to round-trip back and forth to JSON, but I've had no luck, no matter how I try to massage or modify the type. What am I missing? When it compiles, I always get the same runtime error: > let...

Transformations with Data.Aeson.Lens

haskell,lens,aeson
Using aeson-lens, I wrote this program that takes me very close to what I want to achieve: {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Conduit ( simpleHttp ) import Data.Aeson ( decode , Value ) import Data.Maybe ( fromJust ) import Control.Lens ( (^.) ) import Data.Aeson.Lens ( key, nth ) main...

Is it possible to make Traversal an instance of IsString

haskell,types,lens,aeson
I want to use string literal as Traversal, but I am a bit lost in types. Is it possible to create this instance? import Control.Lens import Data.Aeson import Data.Aeson.Lens import Data.String import Data.Default {- Having: key' :: AsValue t => Text -> Traversal' t (Maybe Value) _JSON :: (ToJSON a,...

Pattern Match Vector Value in Data.Aeson

haskell,aeson
I am using Data.Aeson to parse JSON to my custom type. I try to pattern match Vector Value (Array) in my FromJSON instance, but don't know how I can do it. JSON value key can have a value of a String, a list of String or a list of list...

Isolate a single value from a nested JSON response in Aeson

haskell,aeson
I'm working with a couple of JSON based APIs and the vast majority of the time I only need to extract a single value from the JSON response. E.g. with {"foo":"xyz","bar":"0.0000012"} I only need the value of bar. To accommodate this I've written functions to extract the information I need:...

How to correctly error out in JSON parsing with Data.Aeson

parsing,haskell,monads,applicative,aeson
My type and correponding FromJSON implementation as listed below. The nonEmpty turns a List into a Maybe NonEmpty and I'm trying to correctly deal with the case where the List is indeed empty and I have to abort the parsing. This parsing is actually done inside of parseJsonBody, which means...

Parsing JSON data from a URL in Haskell using Aeson

json,haskell,aeson
I'm trying to create a web app in Haskell that takes some JSON input from a URL. {-# LANGUAGE OverloadedStrings, DeriveGeneric #-} import Data.Aeson as Q import Data.Text import Control.Applicative import Control.Monad import qualified Data.ByteString.Lazy as B import Network.HTTP.Conduit (simpleHttp) import GHC.Generics --import Data.DateTime data Temperatures = Temperatures { date...

How to inspect parsed Aeson Value?

haskell,aeson
How can I browse large Aeson Values? I know there should be a string I'm interested in nested somewhere in the structure. How can I find it? So far I only know how to query constructor and found out it's an Array. How can I dig deeper than that? >...

how to handle capital case in JSON?

haskell,aeson
This is a stupid question, and I have tried to understand from different tutorials. When having a JSON with capital case Haskell crashes as explained by others (https://mail.haskell.org/pipermail/beginners/2013-October/012865.html). As suggested it could be solved with deriving from deriveFromJSON. DeriveJSON requires a function input, how should I write the derive statement...