jsonfilter/docs/filter.md
2024-02-13 09:53:20 +01:00

2.5 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"}

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" }}

$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" }}

$in

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 }}

$regex

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

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

$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" }}