Skip to main content

A small, typed JSON frame (envelope) for API responses and messages.

Project description

jsonframe

A tiny, opinionated helper for consistent JSON API response frames.

jsonframe standardizes how APIs return successful responses, collections, pagination metadata, and errors — without dragging in heavy specs or forcing a framework.


Design goals

  • Responses are always JSON objects (never top-level arrays)
  • Predictable structure across services
  • Minimal cognitive load for newcomers
  • No success: true flags — HTTP status codes already exist
  • Small enough to understand in one sitting

Core response rules

Success

{
  "data": ...,
  "meta": { ... }
}
  • data contains the business payload (object, list, scalar, or null)
  • meta contains non-business metadata (optional, always an object). Typical examples include pagination info, request IDs, timing data, or feature flags — never domain data.

Error

{
  "error": {
    "code": "validation_error",
    "message": "Invalid request payload"
  },
  "meta": { ... }
}
  • Errors are represented by a single error object (no arrays, no partial failures)
  • HTTP status code communicates severity
  • meta is optional
  • Additional fields may be included for diagnostics (e.g. context, trace_id)

Examples

Example of success payload and framed result

Given the user object and request_id:

from jsonframe import ok

user = {
  "id": 42, 
  "name": "Ada Lovelace", 
  "email": "ada@example.com", 
  "role": "admin"
}
meta={"request_id": "req_123"}

return ok(data=user, meta=meta)

Result:

{
  "data": {
    "id": 42,
    "name": "Ada Lovelace",
    "email": "ada@example.com",
    "role": "admin"
  },
  "meta": {
    "request_id": "req_123"
  }
}

Example error response

from jsonframe import error

return error(
    code="not_found",
    message="User not found",
    context={"user_id": 42},
    trace_id="9f3c2a8e7d",
    meta={"request_id": "req_123"},
)
{
  "error": {
    "code": "not_found",
    "message": "User not found",
    "context": {
      "user_id": 42
    },
    "trace_id": "9f3c2a8e7d"
  },
  "meta": {
    "request_id": "req_123"
  }
}

Installation

Core package

uv add jsonframe

Core dependency:

  • pydantic >= 2.0 (used for lightweight validation and serialization)

Optional FastAPI integration

FastAPI helpers are optional and not installed by default. FastAPI wraps error payloads under detail; http_error() applies this automatically without changing the core error shape.

uv add "jsonframe[fastapi]"

This installs:

  • fastapi
  • starlette

Usage

Success response

from jsonframe import ok

return ok(data={"id": 1, "name": "Ada"})

Empty success

from jsonframe import ok

return ok()

List response

from jsonframe import ok

return ok(data=[{"id": 1}, {"id": 2}])

Paginated list

from jsonframe import ok_paged

return ok_paged(
    data=[{"id": 1}, {"id": 2}],
    total=120,
    limit=20,
    offset=40,
)

Result:

{
  "data": [...],
  "meta": {
    "page": {
      "total": 120,
      "limit": 20,
      "offset": 40
    }
  }
}

Error response

from jsonframe import error

return error(
    code="validation_error",
    message="Invalid input",
    context={"field": "email"},
)

FastAPI helpers (optional)

Returning framed JSON with status code

ok() returns a plain JSON-serializable dict; json_frame() converts it into a FastAPI Response.

from jsonframe.fastapi import json_frame
from jsonframe import ok

return json_frame(
    ok({"id": 1}), 
    status_code=200
)

Raising framed HTTP errors

from jsonframe.fastapi import http_error

raise http_error(
    404,
    code="not_found",
    message="User not found",
    context={"user_id": 42},
)

What jsonframe is not

  • Not a full JSON:API implementation
  • Not a validation framework
  • Not a transport abstraction
  • Not a replacement for OpenAPI or HTTP semantics

When to use jsonframe

  • Internal APIs
  • BFFs
  • Microservices
  • AI / LLM-backed services
  • Teams that want consistency without ceremony

Philosophy

jsonframe is intentionally small.

It standardizes structure, not business logic.
If you can’t explain your API responses by pointing to this README, the library is doing too much.


License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonframe-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonframe-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonframe-0.1.0.tar.gz.

File metadata

  • Download URL: jsonframe-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for jsonframe-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e515ddaaf5860572e77dcbfa5c717d97e01af6f7188cbebf470b006706b78245
MD5 ed6ffc82681ac9eabc712ec96ef5add5
BLAKE2b-256 ee74d6b505e6090fc358e813b426d3a4ac87aa528b5147bf2975ee55fa0316e4

See more details on using hashes here.

File details

Details for the file jsonframe-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonframe-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for jsonframe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8acfe344825d5b2cd00b7be95716c11d9de56f29779ff933710cf3cd89cbcfbe
MD5 89473706cbe5d380991230d3b36c0425
BLAKE2b-256 b4c7274244912cff4fbd5ddac3f1bb83b8a4f52556d277c04a3c19556e3a500d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page