CQL2 JSON Pydantic

Pydantic v2 models for building CQL2 JSON filters.

Use these docs by intent:

Quick start

pip install cql2json-pydantic
from cql2json_pydantic import BinaryComparisonPredicate, BinaryComparisonPredicateOp, PropertyRef

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

Output:

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

Use model_dump_json(indent=2, exclude_none=True) for formatted JSON, or model_dump(mode="json", exclude_none=True) for a JSON-compatible Python dictionary.

See more filter examples and their JSON output.

For spatial filters, construct a geometry directly from coordinates:

import cql2json_pydantic as cql

point = cql.Point((-115.81, 37.24))
print(point.model_dump_json(exclude_none=True))
{"type":"Point","coordinates":[-115.81,37.24]}

See Build geometries for all seven geometry types, coordinate nesting, and validation.