Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Request Parsing

From Leeroopedia
Knowledge Sources
Domains API_Design, Request_Processing, ORM
Last Updated 2026-10-11 00:00 GMT

Overview

A request processing pattern that accepts declarative JSON structures describing desired data operations and resolves them into structured query/write operations with authentication, validation, and transaction management.

Description

Request Parsing in the APIJSON context is the process of receiving a JSON object that declaratively specifies what data to retrieve or modify, and transforming it into a series of database operations. Unlike traditional REST APIs where each endpoint is hardcoded, APIJSON treats the entire JSON body as a request tree where each node represents a table operation with conditions, joins, and subqueries.

The pattern solves the N+1 endpoint problem: instead of defining separate API endpoints for every combination of tables and filters, a single endpoint accepts arbitrary JSON structures and dynamically generates the necessary SQL. Global keywords prefixed with @ (such as @database, @role, @format, @method) control cross-cutting concerns, while table-keyed objects define per-table operations.

Key aspects include:

  • Global keyword extraction (@format, @database, @schema, @role, @method) before processing
  • Authentication and content verification before any database access
  • Transaction wrapping for write operations (POST/PUT/DELETE)
  • Recursive traversal of the request tree, with each node producing its own SQL and response fragment
  • Unified response assembly with status codes and optional key formatting

Usage

Use this principle when designing an API layer that needs to support flexible, client-defined queries against multiple tables without predefining every endpoint. It is particularly suited for:

  • Internal tooling where the client knows the data model
  • Mobile applications that need to fetch complex nested data in a single round trip
  • Systems where the API surface should remain stable while the data model evolves

Theoretical Basis

The request tree is a declarative specification that maps to a DAG (Directed Acyclic Graph) of database operations:

# Abstract algorithm (NOT real implementation):
def parse_request(request_json):
    global_keywords = extract_at_keys(request_json)  # @format, @database, @role, etc.
    verify_login(visitor)
    verify_content(request_json, request_method)
    
    begin_transaction()  # Only for write methods
    
    try:
        response = recursively_parse(request_json, parent_path=None)
        commit_transaction()
    except Exception:
        rollback_transaction()
        response = build_error_response(exception)
    
    response = format_response(response, global_keywords)
    return response

def recursively_parse(node, parent_path):
    for key, value in node.items():
        if is_table_key(key):
            sql_config = build_sql_config(key, value)
            sql = generate_sql(sql_config)
            verify_access(sql_config, role)
            result = execute_sql(sql)
            merge_result(response, key, result)
        elif is_array_key(key):  # key[]
            handle_paginated_query(key, value)
        elif key.endswith("@"):  # reference key
            resolve_reference(key, value)
        elif key.endswith("()"):  # function call
            execute_function(key, value)

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment