aeson-match-qq: Declarative JSON matchers.

[ bsd2, library, web ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.2.2, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2, 1.4.3, 1.5.0, 1.5.1, 1.5.2, 1.5.3, 1.6.0, 1.6.1, 1.7.0, 1.8.0, 1.9.0, 2.0.0
Change log CHANGELOG.markdown
Dependencies aeson, attoparsec, attoparsec-aeson, base (>=4.14 && <5), bytestring, case-insensitive, containers, either, ghc-hs-meta, pretty, scientific, template-haskell, text, unordered-containers, vector [details]
Tested with ghc ==9.8.4
License BSD-2-Clause
Copyright Matvey Aksenov 2022
Author
Maintainer matvey.aksenov@gmail.com
Uploaded by MatveyAksenov at 2026-11-30T09:43:25Z
Category Web
Home page https://github.com/supki/aeson-match-qq#readme
Bug tracker https://github.com/supki/aeson-match-qq/issues
Source repo head: git clone https://github.com/supki/aeson-match-qq
Distributions
Downloads 3064 total (54 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2026-11-30 [all 1 reports]

Readme for aeson-match-qq-2.0.0

[back to package description]

aeson-match-qq

Declarative JSON matchers.

Motivation

When testing an HTTP service that spouts JSON documents, it's often inconvenient to write out the whole response as the expected value. Sometimes, you only care about a couple specific properties, or simply want to ensure that the response has certain structure. This package provides a quasiquoter to define declarative matchers using a variation of the familiar syntax.

Features

Basic JSON construction

Since this package is heavily inspired by aeson-qq, the parser tries to follow its behavior and has support for optional quotes for simple keys and the #{exp} syntax for Haskell expression interpolation. It currently does not support variable keys though and there are no plans to add them.

Spread-like syntax for arrays and objects

This syntax allows you to match only the part of the structure you care about:

[qq| [1, 2, 3, ...] |] matches arrays starting with [1, 2, 3].

[qq| {foo: 1, bar: 2, ...} |] matches objects that are supersets of {foo: 1, bar: 2}

Holes

Holes are placeholders that match anything:

[qq| _ |] matches any JSON document.

[qq| {foo: _} |] matches any object that has the foo property.

A matcher returns JSON values, referenced by named holes as Captures names, indexed by their names at the type level. Enable -XTypeApplications to look up captures by name with extractValue or extractDecoded:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeApplications #-}

import Aeson.Match.QQ
import qualified Data.Aeson as Aeson
import Data.List.NonEmpty (NonEmpty)

matcher :: Matcher '["age", "name"]
matcher =
  [qq|
    { name: _name : string
    , age: _age : number
    }
  |]

-- lookup up the raw Aeson.Value
extractName :: Aeson.Value -> Either (NonEmpty Error) Aeson.Value
extractName document = extractValue @"name" <$> match matcher document

-- lookup any type that implements `Aeson.FromJSON`
extractAge :: Captures '["age", "name"] -> Either String Int
extractAge = extractDecoded @"age"

In Captures names, the names appear in ascending order. Each named hole must be unique throughout a matcher; this is checked at compile time. Anonymous _ holes may be repeated and are not captured.

Case-insensitive strings

A small addition to the JSON syntax that allows string matches that ignore case:

[qq| (ci) "foo" |] matches any of "Foo", "fOO", or "FOO".

Unordered arrays

A small addition to the JSON syntax that allows array matches that ignore the order of the elements, for example:

[qq| (unordered) [1,2,3] |] matches [3, 1, 2].