aeson-jsonpath
Parse and run JSONPath queries on Aeson documents
https://github.com/taimoorzaeem/aeson-jsonpath
| LTS Haskell 24.41: | 0.3.0.2@rev:2 |
| Stackage Nightly 2026-05-19: | 0.4.0.0 |
| Latest on Hackage: | 0.4.0.0 |
MIT licensed by Taimoor Zaeem
Maintained by Taimoor Zaeem
This version can be pinned in stack with:
aeson-jsonpath-0.4.0.0@sha256:58f3dc46c07d5c41aa8f04275257ebdcf17c9f5cb32f3f87a1db7b4864c19adc,4307Module documentation for 0.4.0.0
Depends on 8 packages(full list with versions):
aeson-jsonpath
Run RFC 9535 compliant JSONPath queries on Data.Aeson.
Roadmap
- [x] Selectors
- [x] Name Selector
- [x] Index Selector
- [x] Slice Selector
- [x] Wildcard Selector
- [x] Filter Selector
- [x] Segments
- [x] Child Segment
- [x] Descendant Segment
- [x] Normalized Paths
- [ ] Function Extensions
- [ ]
length() - [ ]
count() - [ ]
match() - [x]
search() - [ ]
value()
- [ ]
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[[email protected] < 2015]" json
Right [
Object (fromList [
("director",String "Xavier Dolan"),
("title",String "Laurence Anyways"),
("year",Number 2012.0)
])
]
ghci> queryLocated "$.shop.movies[[email protected] == '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 :rocket:.
[!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.
Changes
Change Log
All notable changes to this package are documented in this file. This project adheres to Haskell PVP versioning.
0.4.0.0
- #82, Implement
search()function from function extensions - #83, Improve parse errors
0.3.0.2
- #43, Fix spaces not allowed in relative query and singular query segments
- #50, Fix wrong normalized path with descendant segment
- #30, Fix escape characters not handled properly in normalized path
0.3.0.1
- #31, Fix
test/cts.jsonnot included in hackage bundle
0.3.0.0
- Always return
Vector Value - Implement Filter Selector
- Introduce JSONPath Standard Compliance Testing, See: test-suite
- Add function
queryLocatedwhich also returns node locations
0.2.0.0
- Remove dependency on
protolude - Fix parsing bug with Wildcard Selector
- Implement Descendant Segment
- Fix allowed characters in the
member-name-shorthand - Add
QuasiQuoterfor compile-time syntax checking
0.1.0.0
- Initial Release