Run RFC 9535 compliant JSONPath queries on Data.Aeson.
- Selectors
- Name Selector
- Index Selector
- Slice Selector
- Wildcard Selector
- Filter Selector
- Segments
- Child Segment
- Descendant Segment
- Normalized Paths
- Function Extensions
I have decided not to implement Function Extension yet. Please open an issue or discussion if you'd like to see them implemented.
{-# 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")
]
{-# 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
}
]
}
}|]
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 []
-- get all values with key "director", recursively
ghci> query "$..director" json
Right [
String "Panos Cosmatos",
String "Xavier Dolan"
]
ghci> query "$[2:5]" [aesonQQ| [1,2,3,4,5,6] |]
Right [
Number 3.0,
Number 4.0,
Number 5.0
]
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)
])
)
]
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
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.
Please report any bugs you encounter by opening an issue.