aeson-jsonpath: Parse and run JSONPath queries on Aeson documents

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

RFC 9535 compliant JSONPath parsing and querying package. JSONPath is similar to XPath for querying XML documents.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.3.0.1
Change log CHANGELOG.md
Dependencies aeson (>=2.0.3 && <2.3), base (>=4.9 && <4.22), parsec (>=3.1.11 && <3.2), scientific (>=0.3.4 && <0.4), template-haskell (>=2.12 && <2.24), text (>=1.2.2 && <2.2), vector (>=0.11 && <0.14) [details]
Tested with ghc ==9.4.8, ghc ==9.6.6, ghc ==9.8.4, ghc ==9.10.1
License MIT
Author Taimoor Zaeem
Maintainer Taimoor Zaeem <mtaimoorzaeem@gmail.com>
Category JSON, Text, Web
Home page https://github.com/taimoorzaeem/aeson-jsonpath
Bug tracker https://github.com/taimoorzaeem/aeson-jsonpath/issues
Source repo head: git clone https://github.com/taimoorzaeem/aeson-jsonpath
Uploaded by taimoorzaeem at 2025-01-15T19:28:19Z
Distributions LTSHaskell:0.2.0.0, Stackage:0.3.0.1
Downloads 78 total (68 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for aeson-jsonpath-0.3.0.0

[back to package description]

aeson-jsonpath

ci-badge hackage-docs

Run RFC 9535 compliant JSONPath queries on Data.Aeson.

Roadmap

  • Selectors
    • Name Selector
    • Index Selector
    • Slice Selector
    • Wildcard Selector
    • Filter Selector
  • Segments
    • Child Segment
    • Descendant Segment
  • Node Locations (Normalized Path)
  • Function Extensions
  • Setting Values (Non-RFC)

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": "Lawrence 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"
]

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 "Lawrence Anyways"),
    ("year",Number 2012.0)
  ])
]

ghci> queryLocated "$.shop.movies[?@.director == 'Xavier Dolan']" json
Right [
  (
    "$['shop']['movies'][1]",
    Object (fromList [
      ("director",String "Xavier Dolan"),
      ("title",String "Lawrence 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.