MyNixOS website logo
Description

Parse and run JSONPath queries on Aeson documents.

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

aeson-jsonpath

Build hackage-docs Donate Compliance

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

I have decided not to implement Function Extension 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"
]

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

ghci> queryLocated "$.shop.movies[?@.director == 'Panos Cosmatos']" json
Right [
  (
    "$['shop']['movies'][0]",
    Object (fromList [
      ("director",String "Panos Cosmatos"),
      ("title",String "Mandy"),
      ("year",Number 2018.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.

Metadata

Version

0.3.0.2

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows