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

[ bsd3, json, library, text, web ] [ Propose Tags ] [ Report a vulnerability ]

Tiny library to get fields from JSON format


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6
Change log CHANGELOG.md
Dependencies aeson (>=1.2 && <3), base (>=4.7 && <5), lens (>=4.15 && <6), lens-aeson (>=1.0 && <1.3), text (>=1.2 && <2.1) [details]
Tested with ghc ==8.10.7 || ==9.0.2 || ==9.2.2
License BSD-3-Clause
Copyright (c) 2019, Bogdan Neterebskii
Author Bogdan Neterebskii
Maintainer bog2dan1@gmail.com, kolmax94@gmail.com
Category Text, Web, JSON
Home page https://github.com/biocad/aeson-picker#readme
Bug tracker https://github.com/biocad/aeson-picker/issues
Source repo head: git clone https://github.com/biocad/aeson-picker
Uploaded by maksbotan at 2022-03-23T13:34:19Z
Distributions LTSHaskell:0.1.0.6, NixOS:0.1.0.6
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 3999 total (37 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-03-23 [all 1 reports]

Readme for aeson-picker-0.1.0.6

[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.