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
5
votes
3 answers

Aeson: parse enumerated data types

How can I declare an instance of FromJSON of the following data type: data Privacy = Everyone | AllFriends | FriendsOfFriends | Self So that the following string to enumerated data type is honored: "EVERYONE" ->…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
5
votes
1 answer

Aeson : generics with default values

Today I wanted to solve next problem. Assume that we have typeclass DataWithDefault defined as class DataWithDefault a where defaultValue :: a And we have data Example defined as data Example = Example { field1 :: Text , field2 ::…
d12frosted
  • 1,280
  • 12
  • 33
5
votes
2 answers

Haskell Date parsing

I'm trying to make a FromJSON instance for Data.Time.Calendar's Day type. I am confused about the types, and this seems like a common enough situation that it ought to be solved. So the Day type represents a modified Julian date. And the…
nomen
  • 3,626
  • 2
  • 23
  • 40
5
votes
2 answers

Extracting Values from JSON using lens-aeson

I just read the tutorial at https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms, and I have successfully written a query into a json bytestring. However, I am not getting the kind of result value I want. I'd like to do something along…
nomen
  • 3,626
  • 2
  • 23
  • 40
5
votes
1 answer

Navigating JSON objects in a generic way in Haskell

My goal is to write a program in Haskell that takes the name of a json file and interprets the rest of the arguments as a path to navigate that json file by and print the value navigated to. The problem is because JSON can contain multiple value…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
5
votes
1 answer

Haskell, Aeson - how to debug instances?

I have a complex nested json, which i'm trying to parse with Aeson and Attoparsec, into my custom types. Based on info from questions: Haskell, Aeson & JSON parsing into custom type, Aeson: How to convert Value into custom type? and some info from…
sigrlami
  • 1,822
  • 1
  • 17
  • 35
4
votes
1 answer

Unsupported type UString using Aeson with MongoDB BSON

I am attempting to use Data.Aeson.TH deriveJSON to generate the ToJSON and FromJSON instances for MongoDB Data.Bson. At the moment I am using: $(deriveJSON id ''Data.Bson.Field) $(deriveJSON id ''Data.Bson.Value) $(deriveJSON id…
Toby Hede
  • 36,755
  • 28
  • 133
  • 162
4
votes
1 answer

How do I distinguish negative zero with Aeson?

Haskell distinguishes negative zero: ghci> (isNegativeZero (0 :: Float), isNegativeZero (-0 :: Float)) (False,True) JSON also allows for distinguishing them, since both "0" and "-0" are valid, syntactically. But Aeson throws away the sign…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
4
votes
1 answer

Parsing nested arrays in Aeson

Im struggling to parse the below JSON using the Aeson library. Im only interested in getting file1 but I cant seem to manage it. Does anyone have suggestions? The JSON {"files":[["file1.wav",["file2.jpg","file3.jpg"]]]} My code data File = File…
revilotom
  • 75
  • 5
4
votes
0 answers

How to filter Array values using Data.Lens.Aeson before parsing them?

I have an incoming JSON which looks like: { "somekey": [ { "objectType": "typeA" , "key1": "val1" , "key2": "val2" } , { "objectType": "typeB" , "key3": "val3" , "key4": "val4" } ] While parsing, I want to deal…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
4
votes
1 answer

When parsing JSON with Aeson, why is Maybe treated differently when it's in a type parameter?

Suppose we have some data classes {-# LANGUAGE DeriveGeneric, DuplicateRecordFields #-} import Data.Aeson import Data.ByteString.Lazy.Char8 import GHC.Generics data Foo a = Foo { payload :: a } deriving (Show, Generic) instance ToJSON a =>…
vozman
  • 1,198
  • 1
  • 14
  • 19
4
votes
1 answer

How to parse a JSON string using Aeson that can be one of two different types

I'm currently struggling to parse some JSON data using the aeson library. There are a number of properties that have the value false when the data for that property is absent. So if the property's value is typically an array of integers and there…
4
votes
1 answer

What are FromJSON1 and ToJSON1 used for in aeson?

Aeson provides FromJSON1 and ToJSON1 type classes. These are similar to the Eq1 and Show1 classes defined in the Data.Functor.Classes module. My understanding of the Eq1 and Show1 classes is that they are needed to be able to express constraints on…
illabout
  • 3,517
  • 1
  • 18
  • 39
4
votes
2 answers

Convert constructor in record to custom json string in aeson haskell

I would like to convert my json to below format. And to convert from below format to my record. Please check the code that I have written below. { "uid" : "bob", "emailid" : "bob@bob.com", "email_verified" : "Y" // "Y" for EmailVerified…
navin
  • 165
  • 1
  • 12
4
votes
2 answers

Parsing problematic JSON with Aeson

I am trying to parse JSON objects, which are generally of the form { "objects": [a bunch of records that can assume a few different forms], "parameters": [same deal], "values": { "k1": "v1", "k2": "v2", …
user4601931
  • 4,982
  • 5
  • 30
  • 42