@hackage / aeson-jsonpath

Parse and run JSONPath queries on Aeson documents

Latest0.4.2.0

About

Metadata

  • Last updated , by taimoorzaeem
  • License MIT
  • Categories JSON, Web Development, Text Processing
  • Maintained by: Taimoor Zaeem <mtaimoorzaeem@gmail.com>

  • Lottery factor: 1

Links

Installation

Tested Compilers

  1. 9.12.2
  2. 9.10.1
  3. 9.8.4
  4. 9.6.6
  5. 9.4.8

Readme

aeson-jsonpath

Build hackage-docs Compliance

Run RFC 9535 compliant JSONPath queries on Data.Aeson.

Roadmap

Function Extensions are not finished yet. Please open an issue or discussion if you'd like to see them implemented.

Quick Start

{-# LANGUAGE QuasiQuotes #-}
import Data.Aeson           (Value (..))
import Data.Aeson.QQ.Simple (aesonQQ)
import Data.Aeson.JSONPath  (query, queryLocated, jsonPath)

track = [aesonQQ| { "artist": "Duster", "title": "Earth Moon Transit" } |]

ghci> query "$.artist" track -- child member shorthand
Right [String "Duster"]

ghci> queryLocated "$.*" track -- child wildcard segment
Right [
  ("$['artist']", String "Duster"),
  ("$['title']", String "Earth Moon Transit")
]

More Examples

{-# LANGUAGE QuasiQuotes #-}
import Data.Aeson           (Value (..))
import Data.Aeson.QQ.Simple (aesonQQ)
import Data.Aeson.JSONPath  (query, queryLocated, jsonPath)

json = [aesonQQ| {
  "shop": {
    "movies": [
      {
        "title": "Mandy",
        "director": "Panos Cosmatos",
        "year": 2018
      },
      {
        "title": "Laurence Anyways",
        "director": "Xavier Dolan",
        "year": 2012
      }
    ]
  }
}|]
Child Segment
ghci> query "$.shop.movies[0].title" json
Right [String "Mandy"]

ghci> query "$.shop.movies[0].*" json
Right [
  String "Mandy",
  String "Panos Cosmatos",
  Number 2018.0
]

ghci> query "$['shop']['new-movies']" json
Right []
Descendant Segment
-- get all values with key "director", recursively
ghci> query "$..director" json
Right [
  String "Panos Cosmatos",
  String "Xavier Dolan"
]
-- query along with locations
ghci> queryLocated "$..director" json
Right [
  ("$['shop']['movies'][0]['director']",String "Panos Cosmatos"),
  ("$['shop']['movies'][1]['director']",String "Xavier Dolan")
]
Slice Selector
ghci> query "$[2:5]" [aesonQQ| [1,2,3,4,5,6] |]
Right [
  Number 3.0,
  Number 4.0,
  Number 5.0
]
Filter Selector
ghci> query "$.shop.movies[?@.year < 2015]" json
Right [
  Object (fromList [
    ("director",String "Xavier Dolan"),
    ("title",String "Laurence Anyways"),
    ("year",Number 2012.0)
  ])
]

ghci> queryLocated "$.shop.movies[?@.director == 'Panos Cosmatos']" json
Right [
  (
    "$['shop']['movies'][0]",
    Object (fromList [
      ("director",String "Panos Cosmatos"),
      ("title",String "Mandy"),
      ("year",Number 2018.0)
    ])
  )
]
Search Function
ghci> query "$.shop.movies[?search(@.director, 'Tarantino|Dolan')]" json
Right [
  Object (fromList [
    ("director",String "Xavier Dolan"),
    ("title",String "Laurence Anyways"),
    ("year",Number 2012.0)
  ])
]
QuasiQuoter

The functions queryQQ and queryLocatedQQ can be used with the jsonPath quasi quoter.

queryQQ [jsonPath|$.shop.movies|] json -- compiles successfully

queryQQ [jsonPath|$.shop$$movies|] json -- compilation error, doesn't parse

Testing

It is tested using 10000+ lines test suite given by jsonpath-compliance-test-suite 🚀.

[!NOTE] All tests pass except tests related to function extensions which we have not implemented yet.

Development

Please report any bugs you encounter by opening an issue.