BSD-3-Clause licensed by Bogdan Neterebskii
This version can be pinned in stack with:aeson-picker-0.1.0.6@sha256:01acf227c011c495d6489adaf9170fdd2614d9d4f409796843e7676d978e0604,1404

Module documentation for 0.1.0.6

aeson-picker

Travis hackage hackage-deps

Tiny library to get fields from JSON format

Common use is the following:

JSON |-- FIELDS :: EXPECTED TYPE
Text |-- [Text] :: a

So operator (|--) gets JSON (represented as Text), “route” to field inside JSON (represented as [Text]), and tries to parse field from JSON with described route to expected type. If expected type can be deduced with type checker then it can be dropped down.

A little bit safer operator is (|-?). It returns not a but Maybe a.

Examples

First, add extension (to not pack String to Text every time):

ghci>:set -XOverloadedStrings

Then you can try something simple (empty list means that you try to parse Value from the top JSON-level):

ghci>"5" |-- [] :: Int
5
ghci>"5" |-- [] :: Float
5.0
ghci>"5" |-- [] :: String
"*** Exception: Data.Aeson.Picker: could not pick field with path: []

But what if field you are looking for somewhere inside JSON? That’s why are you here.

Let’s try to get something from inside JSON:

ghci>"{\"a\": 5}" |-- ["a"] :: Int
5

But be sure that the field is presented inside JSON:

ghci>"{\"a\": 5}" |-- ["b"] :: Int
*** Exception: Data.Aeson.Picker: could not pick field with path: ["b"]

We can go deeper (as deep as you want):

ghci>"{\"outer\": {\"inner\": [1,2,3]}}" |-- ["outer", "inner"] :: [Int]
[1,2,3]

But be sure that you JSON is really valid (by specification key in JSON should be String):

ghci>"{a: 5}" |-- ["a"] :: Int
*** Exception: Data.Aeson.Picker: input json is not valid

If you want more “safe” picker, you can use another operator:

ghci>"5" |-? [] :: Maybe Int
Just 5
ghci>"{\"a\": 5}" |-? ["a"] :: Maybe Int
Just 5
ghci>"{\"a\": 5}" |-? ["b"] :: Maybe Int
Nothing

In current logic even operator (|-?) will throw error if JSON is not valid:

ghci>"{a: 5}" |-? ["a"] :: Maybe Int
*** Exception: Data.Aeson.Picker: input json is not valid

You can open issue if you do not think that it is right logic.

Changes

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[0.1.0.6] - 2022-03-23

Changed

  • Support aeson-2 and other GHC-9 related packages.

[0.1.0.5] - 2019-09-07

Changed

  • Upper bounds for lens-aeson.

[0.1.0.0] - 2018-01-22

Added

  • Initial version for picking.