Skip to main content

simple_json_func

simple_json_func is a Python library for evaluating expressions stored as JSON. Register Python callables by name, then use JSON objects to call them, pass arguments, and compose nested expressions. The library has no third-party runtime dependencies.

Installation

Use a standard Python installation with pip. From the repository root, run:

python -m pip install .

Quick start

import json
from operator import add

from simple_json_func import JsonFunc

evaluator = JsonFunc(is_root=True, can_echo=False)
evaluator.bind("add", add)

expression = json.loads('''
{
  "_f": "add",
  "_c": [2, {"_f": "add", "_c": [3, 4]}]
}
''')
print(evaluator.calc(expression))  # 9

calc() takes a decoded Python value. Use json.loads() for JSON text or json.load() for an open JSON file. Use trusted configurations: an expression can invoke any callable registered on its evaluator.

Expression format

An object with a non-null _f refers to a registered function. Its _c field controls how the function is used:

_c value Default behavior
Array Unpack it as positional arguments; use [] for a call with no arguments.
Object Unpack it as keyword arguments.
Other non-null value Pass it as one positional argument.
Omitted or null Return the registered callable without invoking it.

Arrays and ordinary objects (those without a non-null _f) are evaluated recursively. Scalar values pass through unchanged. A reference to an unregistered function raises KeyError.

Built-in functions

Set is_root=True to register these functions. With is_root=False, the evaluator starts with no registered functions.

Function Behavior
@calc Evaluates the raw _c value as an expression.
@if Takes the raw arguments [cond, vt, vf] and evaluates only the selected branch. It tests cond using Python truthiness without evaluating it as an expression.
@map Takes a callable func and a list lis, then returns a list of results, one per item.
@static Returns the entire expression object unchanged, including the @static wrapper.
@json Evaluates _c, then serializes the result to a JSON string with json.dumps().
@echo Evaluates _c, records its string representation, and returns None. With can_echo=True, it also sends the text to print_hook (which defaults to print).

For example, pass a callable reference to @map and use @if to skip an unused branch. This example reuses evaluator from the quick start:

evaluator.bind("double", lambda value: value * 2)

result = evaluator.calc({
    "_f": "@if",
    "_c": [
        True,
        {"_f": "@map", "_c": [{"_f": "double"}, [1, 2, 3]]},
        {"_f": "not_registered", "_c": []},
    ],
})
print(result)  # [2, 4, 6]

Registration and output

Use bind(name, func, no_fill=False, late=False) to register a callable. By default, calc() evaluates _c before calling the function and returns the function's result as-is.

  • no_fill=True passes _c as a single argument, even when it is an array or an object.
  • late=True passes raw arguments and evaluates the returned value. With both flags enabled, the callable receives the raw _c as one argument and its return value passes through unchanged.
  • late(name) and unlate(name) toggle late evaluation for a name. all_f() lists registered names, and all_late() lists registered late-evaluation names.
  • merge(other, prefix) imports bindings from another JsonFunc instance, prepending prefix to each name. Use "" to leave the names unchanged.
  • get_echo_history() returns the recorded @echo entries, even when immediate output is disabled. flush_echo_history(show=True) sends the joined entries to print_hook and clears them. Pass show=False to clear them without printing.

License

MIT

Metadata

Release files for simple-json-func 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-json-func 0.1.0
File Size Uploaded Provenance Actions
simple_json_func-0.1.0.tar.gz 5.1 kB Without provenance Details for simple_json_func-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-json-func 0.1.0
File Interpreter ABI Platform Actions
simple_json_func-0.1.0-py3-none-any.whl Python 3 none any Details for simple_json_func-0.1.0-py3-none-any.whl

Total release size: 11.2 kB

Release files / simple_json_func-0.1.0.tar.gz

Download URL simple_json_func-0.1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8abff38e0cb26b7ca82d9fda7abaf1b5651be309129106d88d0cb8109bb5122d
BLAKE2b-256 checksum
How to use checksums
cca42da18a064c6e7e571faa118de05e75e85349939544f7a652cc0f0fed9af7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.6 Windows/11

Release files / simple_json_func-0.1.0-py3-none-any.whl

Download URL simple_json_func-0.1.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be1f2040fe2cdd8eab63b9ba2ab71ff34fdcf821f816c40bd780b360f183b57e
BLAKE2b-256 checksum
How to use checksums
99f31870350dfbe75f8a1d48764f0da370195b9150c47f4c331bbf2378ba10a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.6 Windows/11

Security for simple-json-func 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page