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

Copyright | (c) 2024-2025 Taimoor Zaeem
License | MIT
Maintainer | Taimoor Zaeem <mtaimoorzaeem@gmail.com>
Stability | Experimental
Portability | Portable
Safe Haskell | None
Language | Haskell2010

Data.Aeson.JSONPath

Contents

- Using this library
- API
- QuasiQuoter

Description

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

Synopsis

- query :: String -> Value -> Either ParseError (Vector Value)
- queryQQ :: Query -> Value -> Vector Value
- queryLocated :: String -> Value -> Either ParseError (Vector (String, Value))
- queryLocatedQQ :: Query -> Value -> Vector (String, Value)
- jsonPath :: QuasiQuoter

# 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]|]