Principle:APIJSON APIJSON Response Assembly
| Knowledge Sources | |
|---|---|
| Domains | ORM, Response_Formatting, API_Design |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A response construction pattern that assembles parsed query results into a unified JSON response tree with standard status fields and optional key formatting for client consumption.
Description
Response Assembly is the final phase of the APIJSON request pipeline. After all table operations are parsed and executed, the response tree is assembled with:
- Status fields: code (HTTP-style status code) and msg (status message) are added to the root and nested response objects
- Key formatting: When @format is enabled, internal keys (with @, :, -, _ markers) are converted to clean variable names
- Tree mirroring: The response tree mirrors the request structure — each table key in the request has a corresponding key in the response with its data
The status codes follow HTTP conventions:
- 200 - Success
- 400 - Bad request (invalid JSON, missing required fields)
- 401 - Not logged in
- 403 - Forbidden (access denied)
- 404 - Not found
- 500 - Server error
The problem solved is providing a consistent, predictable response format regardless of the request complexity. Every response has code and msg at the root, and data nested under the same keys as the request.
Usage
Use this principle when:
- An API needs consistent status reporting across all operations
- Response keys should be formatted for client consumption
- The response structure should mirror the request structure
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def assemble_response(parsed_tree, is_success, error=None):
if is_success:
response = add_status(parsed_tree, code=200, msg="success")
else:
response = build_error_response(error)
if global_format:
response = format_keys(response)
return response
def add_status(tree, code, msg):
tree["code"] = code
tree["msg"] = msg
for key, value in tree.items():
if isinstance(value, dict):
add_status(value, code, msg) # recursive
return tree
def format_keys(tree):
new_tree = {}
for key, value in tree.items():
new_key = format_key(key, type(value))
if isinstance(value, dict):
new_tree[new_key] = format_keys(value)
elif isinstance(value, list):
new_tree[new_key] = [format_keys(item) for item in value]
else:
new_tree[new_key] = value
return new_tree