First steps

In this tutorial, you will install cql2json-pydantic and build a CQL2 JSON filter.

Install

pip install cql2json-pydantic

Build a filter

from cql2json_pydantic import BinaryComparisonPredicate, BinaryComparisonPredicateOp, PropertyRef

query = BinaryComparisonPredicate(
    op=BinaryComparisonPredicateOp.EQUAL,
    args=[PropertyRef(property="city"), "Toronto"],
)
print(query.model_dump_json(indent=2, exclude_none=True))

Output:

{
  "op": "=",
  "args": [
    {
      "property": "city"
    },
    "Toronto"
  ]
}

model_dump_json() returns a JSON string. Set indent=2 to make it readable; omit indent for compact output. To obtain a Python dictionary for an API client, use query.model_dump(mode="json", exclude_none=True).

Next step

Explore more filter examples and their JSON output, including logical combinations, list membership, spatial predicates, time intervals, and arithmetic expressions.

For spatial filters, follow Build geometries to construct points, lines, and polygons directly from coordinate lists or tuples.