FiveonefourFiveonefour
Fiveonefour Docs
MooseStackTemplatesGuides
Release Notes
Source514
  1. MooseStack
  2. Data Types
  3. JSON Type

On this page

Basic JSON (Unstructured)Typed JSON with ClickHouseJsonPerformance Tuning OptionsConfiguration OptionsExample: Flexible Event DataBenefits of Typed JSONSee Also

JSON Type

The JSON type stores arbitrary JSON data, supporting both unstructured and typed configurations for performance and type safety.

Basic JSON (Unstructured)

For completely dynamic JSON data without a fixed schema:

from typing import Any, Dict class Event(BaseModel):    metadata: Dict[str, Any]    # Basic JSON - accepts any structure    config: Any                 # Basic JSON - fully dynamic

Typed JSON with ClickHouseJson

For better performance and validation, define typed fields within your JSON using ClickHouseJson. This creates a ClickHouse JSON column with explicit type hints for specific paths while still allowing additional dynamic fields.

from typing import Annotated, Optionalfrom pydantic import BaseModel, ConfigDictfrom moose_lib.data_models import ClickHouseJsonfrom datetime import datetime # Define the structure for your JSON payloadclass PayloadStructure(BaseModel):    model_config = ConfigDict(extra='allow')    # Required for JSON types    name: str    count: int    timestamp: Optional[datetime] = None class Event(BaseModel):    id: str    # JSON with typed paths - better performance, allows extra fields    payload: Annotated[PayloadStructure, ClickHouseJson()]

Performance Tuning Options

Configure ClickHouseJson with options to control resource usage:

from typing import Annotated, Optionalfrom pydantic import BaseModel, ConfigDictfrom moose_lib import Key, ClickHouseJsonfrom datetime import datetime class ProductProperties(BaseModel):    model_config = ConfigDict(extra='allow')    category: str    price: float    in_stock: bool class ProductEvent(BaseModel):    event_id: Key[str]    timestamp: datetime    properties: Annotated[ProductProperties, ClickHouseJson(        max_dynamic_paths=128,           # Limit tracked paths        max_dynamic_types=8,             # Limit type variations        skip_paths=("_internal",),       # Exclude specific paths        skip_regexes=(r"^debug_",)       # Exclude paths matching regex    )]

Configuration Options

OptionTypeDescription
max_dynamic_pathsnumberMaximum unique JSON paths to track. Controls memory for variable structures.
max_dynamic_typesnumberMaximum type variations per path. Useful when paths may contain different types.
skip_pathsstring[]Exact JSON paths to ignore (e.g., ["temp", "debug.info"]).
skip_regexpsstring[]Regex patterns for paths to exclude (e.g., ["^tmp\\.", ".*_internal$"]).

Example: Flexible Event Data

With typed JSON, you get the best of both worlds—type safety for known fields and flexibility for dynamic data:

# This data is valid:{    "event_id": "evt_123",    "timestamp": "2025-10-22T12:00:00Z",    "properties": {        "category": "electronics",      # Typed field ✓        "price": 99.99,                 # Typed field ✓        "in_stock": True,               # Typed field ✓        "custom_tag": "holiday-sale",   # Extra field - accepted ✓        "brand_id": 42,                 # Extra field - accepted ✓        "_internal": "ignored"          # Skipped by skip_paths ✓    }}

Benefits of Typed JSON

  • Better Performance: ClickHouse optimizes storage and queries for known paths
  • Type Safety: Validates that specified paths match expected types
  • Flexible Schema: Allows additional fields beyond typed paths
  • Memory Control: Configure limits to prevent unbounded resource usage
When to use each approach
  • Basic JSON (any, Dict[str, Any]): Use when JSON structure is completely unknown or rarely queried
  • Typed JSON (ClickHouseJson): Use when you have known fields that need indexing/querying, but want to allow additional dynamic fields

See Also

  • Nested — Fixed-structure embedded objects
  • Maps — Key-value pairs with uniform types
  • ClickHouse JSON — ClickHouse official documentation
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Data Modeling
Moose Modules
  • Moose OLAP
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Data Types
    • Strings
    • LowCardinality
    • Integers
    • Floats
    • Decimals
    • Booleans
    • Date & Time
    • Network
    • Arrays
    • Maps
    • Nested
    • Tuples
    • Enums
    • Geometry
    • JSON
    • Nullable
    • Aggregates
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework