aeson-jsonpath
Copyright(c) 2024-2025 Taimoor Zaeem
LicenseMIT
MaintainerTaimoor Zaeem <[email protected]>
StabilityExperimental
PortabilityPortable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Aeson.JSONPath

Description

Run JSONPath queries on Aeson Values using methods exported in this module.

Synopsis

Using this library

To use this package, I would suggest that you import this module like:

{-# LANGUAGE QuasiQuotes #-}
import qualified Data.Aeson.JSONPath as JSONPath
import           Data.Aeson.JSONPath (jsonPath)

For this module, consider this json for the example queries

{ "artist": "David Bowie", "title": "Space Oddity" }

API

query :: String -> Value -> Either ParseError (Vector Value) Source #

Use when query string is not known at compile time

>>> query "$.artist" json
Right [String "David Bowie"]

>>> query "$.art[ist" json
Left "failed to parse query: $.art[ist" (line 1, column 7)

For detailed usage examples, see: https://github.com/taimoorzaeem/aeson-jsonpath?tab=readme-ov-file#aeson-jsonpath

queryQQ :: Query -> Value -> Vector Value Source #

Use when query string is known at compile time

artist = queryQQ [jsonPath|$.artist|] json -- successfully compiles

>>> artist
[String "David Bowie"]
artist = queryQQ [jsonPath|$.art[ist|] json -- fails at compilation time

queryLocated :: String -> Value -> Either ParseError (Vector (String, Value)) Source #

Get the location of the returned nodes along with the node

>>> queryLocated "$.title" json
Right [("$['title']",String "Space Oddity")]

queryLocatedQQ :: Query -> Value -> Vector (String, Value) Source #

Same as queryLocated but allows QuasiQuoter

artist = queryLocatedQQ [jsonPath|$.*|] json -- successfully compiles

>>> artist
[("$['artist']",String "David Bowie"),
 ("$['title']",String "Space Oddity")]

QuasiQuoter

jsonPath :: QuasiQuoter Source #

A QuasiQuoter for checking valid JSONPath syntax at compile time

path :: Query
path = [jsonPath|$.store.records[0,1]|]