jsonfilter/docs/filter.md
2025-05-06 23:43:23 +02:00

3.1 KiB

Filter Syntax

Every filter is a JSON object. At its simplest form, a filter acts as a mask, checking if the values of the filter match those of the object being filtered.

{
    "key": "value",
    "nested": {
        "key": "value"
    }
}

This filter would match against an object like:

{
    "key": "value",
    "other": "data",
    "nested": {
        "key": "value"
    }
}

Advanced Filtering with Operators

Operators enable complex filtering by allowing operations on keys and values.

Example: Logical Chaining and Regex

{
    "$or": [
        { "key": { "$regex": "^L" }},
        { "key": { "$regex": "in$" }},
    ]
}

This filter would match all of these objects:

{ "key": "Login" }
{ "key": "Lenin"}

but not:

{ "key": "lol"}

Logical Operators

$and

Chain multiple filters together. All must evaluate to true

{
    "$and": [
        { "key": "val" },
        { "another": "filter" }
    ]
}

$or

Chain multiple filters together. At least one must evaluate to true

{
    "$or": [
        { "key": "val" },
        { "another": "filter" }
    ]
}

$not

Inverts the result of the nested filter expression.

{ "$not": { "key": "value" }}

Comparison Operators

$lt & $lte

Evaluates to true if the value is less than or less than equal the specified value.

{ "key": { "$lt": 5 }}

$gt & $gte

Evaluates to true if the value is greater than or greater than equal the specified value.

{ "key": { "$gt": 5 }}

$ne

Evaluates to true if the value is not equal to the specified value.

{ "key": { "$ne": "value" }}

Note

: The $eq operator is implicit. Example: { "key": "value" }

Array / Object Operators

$in & $contains

Evaluates to true if the value exists in the specified array.

{ "array": { "$in": "value" }}

$nin

Evaluates to true if the value does not exist in the specified array.

{ "array": { "$nin": "value" }}

$exists

Checks wether the key exists in the object.

{"key": { "$exists": true }}
{"key": { "$exists": false }}

$size

Evaluates to true if the array length matches the specified value

{ "array": { "$size": 5 }}

Text Operators

$regex

Evaluates to true if the value matches the regular expression pattern.

{ "key": { "$regex": "^regex" }}

$contains

Evaluates to true if the value contains the text.

{ "key": { "$contains": "text" }}

Misc Operators

$type

Evaluates to true if the value matches the specified type.

{ "key": { "$type": "null" }}
{ "key": { "$type": "string" }}
{ "key": { "$type": "number" }}
{ "key": { "$type": "object" }}
{ "key": { "$type": "array" }}
{ "key": { "$type": "boolean" }}

$range

Evaluates to true if the value is within the specified range. This operator is actually just syntactic sugar for let filter = json!({"$and": [{"key": {"$gte": x}}, {"key": {"$lte": y}}]});.

{"key": { "$range": [x, y] }}