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
11
votes
1 answer

Fault tolerant JSON parsing

I'm using Data.Aeson to parse some JSON into a Record type. From time to time data is added to the JSON and this breaks my code as Aeson complains something to the effect of: expected Object with 21 name/value pairs but got 23 name/value I'd…
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
10
votes
2 answers

Parse JSON with fieldnames that contain reserved keywords

I'm trying to parse the following JSON with aeson. { "data": [ { "id": "34", "type": "link", "story": "foo" }, { "id": "35", "type": "link", "story":…
mb21
  • 34,845
  • 8
  • 116
  • 142
9
votes
1 answer

Haskell: Reusing FromJSON instances with lenses, lens-aeson, and nested JSON

I have been playing with Aeson and the lens package (lens-aeson, migrated from the core lens package), and have been sruggling to get them to work together. As a toy example, I have a type: data Colour = Yellow | Green | Blue and the FromJSON…
jsdw
  • 5,424
  • 4
  • 25
  • 29
9
votes
1 answer

Type signature needs a type that isn't exported by the library

So I was using the aeson library, and thought it would be very useful to have the following function: v .:! f = liftM (fromMaybe mempty) (v .:? f) When I ask GHCi for the type, I get: (.:!) :: (Monoid r, FromJSON r) => Object -> T.Text…
Emil
  • 2,098
  • 11
  • 25
9
votes
1 answer

Streaming parsing of JSON in Haskell with Pipes.Aeson

The Pipes.Aeson library exposes the following function: decode :: (Monad m, ToJSON a) => Parser ByteString m (Either DecodingError a) If I use evalStateT with this parser and a file handle as an argument, a single JSON object is read from the file…
immutablestate
  • 287
  • 1
  • 9
9
votes
2 answers

Parse Array in nested JSON with Aeson

I'm trying to write a FromJSON function for Aeson. The JSON: { "total": 1, "movies": [ { "id": "771315522", "title": "Harry Potter and the Philosophers Stone (Wizard's Collection)", "posters": { "thumbnail":…
mb21
  • 34,845
  • 8
  • 116
  • 142
8
votes
1 answer

Omitting Nothing/null fields in Haskell's Aeson

I have a type {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE MultiWayIf #-} import GHC.Generics import Data.Aeson.TH import Data.Aeson.Types data MyJSONObject = MyJSONObject { name :: String , ptype ::…
user4601931
  • 4,982
  • 5
  • 30
  • 42
8
votes
1 answer

Aeson and Lens with DeriveGeneric and makeLenses - names don't line up

Let's say I have a type Person import GHC.Generics import Data.Text import Data.Aeson import Control.Lens data Person = Person { _firstName :: Text, _lastName :: Text, _age :: Int } deriving (Show, Generic) And I want to…
James Davies
  • 9,602
  • 5
  • 38
  • 42
8
votes
1 answer

How to omit empty lists using aeson deriveJSON?

Using the aeson deriveJSON it is easily to omit Nothing values, e.g.: data Person = Person { ssn :: Maybe Text, phone :: [Text] } $(deriveJSON defaultOptions{omitNothingFields=True} ''Person) I would like to also omit empty lists in…
user868458
  • 81
  • 2
7
votes
4 answers

Why aren't Persistent types instances of ToJSON/FromJSON in Yesod?

It's not that hard to write ToJSON/FromJSON instances for the generated types but still, while you're generating code could you throw that in? Or is there an easy way to make this happen as a Yesod user? (I haven't dug too deep into how TH…
svachalek
  • 1,166
  • 8
  • 18
7
votes
1 answer

Haskell Aeson: How to convert Value into custom type?

Can't find a good example. Appreciate any help. The JSON is as follows: [{ "EXIF:Make": "Canon", "EXIF:Model": "Canon PowerShot S95", "EXIF:Orientation": "Horizontal (normal)", "EXIF:XResolution": 180, "EXIF:YResolution": 180, …
Muchin
  • 4,887
  • 4
  • 23
  • 25
7
votes
3 answers

How to deal with incomplete JSON/Record types (IE missing required fields which I'll later fill in)?

EDIT: For those with similar ailments, I found this is related to the "Extensible Records Problem", something I will personally research more into. EDIT2: I have started to solve this (weeks later now) by being pretty explicit about data types, and…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
7
votes
2 answers

Haskell - generating JSON with aeson gives incorrect order of fields

I am trying to encode a data type into JSON: {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} import Data.Aeson data Trend = Trend { period :: String , africa :: String , americas :: String …
matt
  • 1,817
  • 14
  • 35
7
votes
1 answer

How to correctly error out in JSON parsing with Data.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…
Alexandr Kurilin
  • 7,685
  • 6
  • 48
  • 76
7
votes
1 answer

Automatic derivation of ToJSON for (Map NewtypeOfText v)

I have a Map where the key is a newtype of Text. I would like to automatically (as much as possible) derive ToJSON and FromJSON for this Map. aeson already has instances for ToJSON and FromJSON for Map Text v. My verbose code that works: {-#…
Daniel K
  • 887
  • 6
  • 13
1
2
3
25 26