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 Recursive Object Parsing

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

Overview

A parsing pattern that recursively traverses a declarative JSON request tree, processing each table-keyed node as an independent database operation with conditions, child objects, and function calls.

Description

Recursive Object Parsing treats a JSON API request as a tree of table operations. Each node in the tree (a table-keyed object like "Moment": {}) represents a single database query or write operation. The parser walks the tree depth-first, processing each node by:

  1. Extracting SQL conditions (WHERE clauses) from regular keys
  2. Resolving reference keys (key@) that link to other objects in the tree
  3. Detecting and deferring function call keys (key()) for post-SQL execution
  4. Recursively parsing child table objects (key:{}) and arrays (key[])
  5. Classifying @-prefixed keys as SQL directives (@column, @group, @order, @combine)

This pattern solves the problem of composing complex multi-table queries from a single flat JSON request. Instead of multiple API round trips for each table, the client declares the entire data shape in one request, and the server recursively resolves dependencies.

The key challenge is dependency ordering: reference keys (key@) create dependencies between objects, and the parser must resolve references before generating SQL for dependent tables. The tree is parsed in document order, and unresolved references trigger deferred execution.

Usage

Use this principle when an API needs to support nested, multi-table queries in a single request. It is the core mechanism behind APIJSON's ability to fetch related data (e.g., a Moment and its author User) in one round trip.

Theoretical Basis

The request tree maps to a DAG of database operations:

# Abstract algorithm (NOT real implementation):
def parse_object(name, request, parent_path):
    table, alias = parse_entry(name)  # "Comment:to" -> ("Comment", "to")
    
    sql_request = {}    # WHERE conditions + directives
    function_map = {}   # Function calls (executed after SQL)
    child_map = {}      # Child table results
    
    for key, value in request.items():
        if key.startswith("@"):
            sql_request[key] = value  # Directive (@column, @group, etc.)
        elif key.endswith("@"):
            resolve_reference(key, value, parent_path)  # key@ path reference
        elif key.endswith("()"):
            function_map[key] = value  # Function call
        elif is_array_key(key):  # key[]
            child_map[key] = parse_array(key, value, parent_path)
        elif is_table_key(key):  # key:{}
            child_map[key] = parse_object(key, value, parent_path + "/" + key)
        else:
            sql_request[key] = value  # WHERE condition
    
    sql_config = build_sql_config(table, sql_request, method)
    sql = generate_sql(sql_config)
    result = execute_sql(sql)
    
    # Execute deferred function calls
    for func_key, func_args in function_map.items():
        result[func_key] = execute_function(func_key, func_args, result)
    
    # Merge child results
    for child_key, child_result in child_map.items():
        result[child_key] = child_result
    
    return result

Related Pages

Implemented By

Uses Heuristic

Page Connections

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