Questions tagged [aeson]

A Haskell JSON parsing and encoding library optimized for high performance and easy usage

Aeson is a Haskell JSON parsing and encoding library optimized for high performance and easy usage.

It provides a typeclass-based interface for serializiation and parsing of object hierarchies (see the FromJSON and ToJSON typeclasses) andprovides lazy or strict parsing mechanisms.

Errors can be handled by using Maybe or Either, providing typesafe parsing of any object structure.

In greek mythology, Aeson was the father of Jason, therefore relating the library's name to the format it parses.

383 questions
4
votes
3 answers

How to use Parsers from Aeson with IO

I have data types with many fields that, if not being manually specified by a JSON configuration file, should be randomly set. I am using Aeson to parse the config file. What is the best way to do this? Currently, I am setting values equal to some…
4
votes
1 answer

Custom ToJSON instance for Persistent Key

I am building a web application in Haskell. I am using the persistent library to connect to a postgresql database. I am using the standard schema definition file system where template Haskell is used to generate types from the schema. share…
Harpo
  • 81
  • 3
4
votes
1 answer

How to conditionally parse JSON based on settings in a Reader environment?

Is there any way to pass down a reader environment to the JSON (de)serialisation functions of Aeson? Here's a real-life example of why this could be required? -- JSON instances for decimal -- ORPHAN instances defaultPrecision ::…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
4
votes
1 answer

How to merge Aeson objects?

I have a list of aeson objects like this [object ["key1" .= "value1"], object ["key2" .= "value2"]] and I want to merge them as a single aeson object like this object ["key1" .= "value1", "key2" .= "value2"] This is quite standard when working…
Batou99
  • 869
  • 10
  • 19
4
votes
2 answers

Automatically deriving toJSON/fromJSON using the instances of the inner type of a newtype

In my code I use a lot of newtype declarations, like: newtype PersonName = PersonName { personName :: Text } newtype PetName = PetName { petName :: Text } (In practice I use lenses to avoid the cumbersome names for the accessor functions.) However,…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
4
votes
1 answer

Reduce memory usage of a Haskell program

I have a following program in Haskell: processDate :: String -> IO () processDate date = do ... let newFlattenedPropertiesWithPrice = filter (notYetInserted date existingProperties) flattenedPropertiesWithPrice geocodedProperties <-…
Leonti
  • 10,400
  • 11
  • 43
  • 68
4
votes
1 answer

How to use Data.Text.Lazy.IO to parse JSON files with Aeson

I want to parse all json files in a given directory into a data type Result. So i have a decode function decodeResult :: Data.ByteString.Lazy.ByteString -> Maybe Result I began with Data.Text.Lazy.IO to load file into Lazy ByteString, import…
Yuan Wang
  • 479
  • 2
  • 13
4
votes
1 answer

Haskell Aeson: How to get value out of Parser in IO monad

I'm trying to do JSON parsing in IO: {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Simple import Data.Aeson import Data.Maybe (fromJust) main :: IO () main = do response <- getResponseBody <$> httpJSON "http://localhost:9200" :: IO…
McBear Holden
  • 5,741
  • 7
  • 33
  • 55
4
votes
1 answer

Haskell Data.Decimal as Aeson type

Is it possible to parse a Data.Decimal from JSON using the Aeson package? Suppose I have the following JSON: { "foo": 5.231, "bar": "smth" } And the following record type: data test { foo :: Data.Decimal , bar :: String } deriving…
PiMaker
  • 511
  • 3
  • 16
4
votes
2 answers

Aeson decode JSON object that can be either a string or an int

I'm working with some complexly formatted JSON responses from a REST server. To decode them, I have a couple of data types to handle the different nested objects. For example: ... Other types ... data Profile = Profile { fields :: [KVPair] }…
jkeuhlen
  • 4,401
  • 23
  • 36
4
votes
1 answer

How do I parse this JSON with Aeson?

I have the following JSON snippet: { "weather": [ { "id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04n" } ], "main": { "temp": 271.979, "pressure": 1024.8, "humidity": 100, …
Varun Madiath
  • 3,152
  • 4
  • 30
  • 46
4
votes
3 answers

Aeson: parsing dynamic keys as type field

Let's say there is a JSON like: { "bob_id" : { "name": "bob", "age" : 20 }, "jack_id" : { "name": "jack", "age" : 25 } } Is it possible to parse it to [Person] with Person defined like below? data Person = Person { id …
Eric
  • 2,784
  • 1
  • 20
  • 25
4
votes
1 answer

Possible to generically remove function types from datatype, to allow deriveJSON?

I have several datatypes representing the state of an application. In various places in the datatype, I have embedded functions or monadic actions, eg. data Foo = Foo Int (ActionM String) data Bar = Bar Foo (Maybe Bar) (ActionM ()) I need to encode…
ajp
  • 1,723
  • 14
  • 22
4
votes
1 answer

Pattern Match Vector Value in Data.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 of…
Joris Buchou
  • 128
  • 7
4
votes
1 answer

Haskell ADTs with 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 t =…
stormont
  • 539
  • 5
  • 16