aeson-picker: Tiny library to get fields from JSON format

[ bsd3, json, library, text, web ] [ Propose Tags ]

Tiny library to get fields from JSON format


[Skip to Readme]
Versions 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4
Change log CHANGELOG.md
Dependencies aeson (==1.2.*), base (>=4.7 && <5), lens (>=4.15 && <5), lens-aeson (==1.0.*), text (==1.2.*) [details]
License BSD-3-Clause
Copyright (c) 2018, Bogdan Neterebskii
Author Bogdan Neterebskii
Maintainer bog2dan1@gmail.com
Category Text, Web, JSON
Home page https://github.com/ozzzzz/aeson-picker#readme
Bug tracker https://github.com/ozzzzz/aeson-picker/issues
Source repo head: git clone https://github.com/ozzzzz/aeson-picker
Uploaded by ozzzzz at Tue Feb 13 06:16:29 UTC 2018
Distributions LTSHaskell:0.1.0.4, NixOS:0.1.0.4, Stackage:0.1.0.4, openSUSE:0.1.0.4
Downloads 185 total (28 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-02-13 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for aeson-picker-0.1.0.2

[back to package description]

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.