Gramson
Define operators once. Generate the grammar that validates your JSON expressions.
Gramson compiles YAML operator declarations into recursive JSON Schema grammars. Extend the bundled JSON Logic syntax profile with your application's operators, or define an independent language. Its Python library and CLI generate schemas and validate JSON or YAML expressions; your application supplies evaluation.
Install and validate
Requires Python 3.10 or newer. Install in a virtual environment:
python -m venv .venv
. .venv/bin/activate
python -m pip install gramson
Validate a core expression immediately:
printf '%s\n' '{">=": [{"var": "age"}, 18]}' | gramson validate -
For a saved rule, use gramson validate expression.json or
gramson validate expression.yaml. Validation checks syntax, without evaluating
the rule or requiring its input data. Without --schema, it uses the bundled
core grammar.
Extend the language with a host operator
Suppose your application stores loan eligibility rules as JSON. An illustrative rule requires a credit score of at least 700 and monthly debt payments no greater than 35% of gross monthly income.
Core operators handle the comparisons and arithmetic. Your application adds
creditScore, which looks up an applicant's score in the host system. Save its
syntax declaration in language/custom/credit.yaml:
creditScore:
argumentKind: expression
minArgs: 1
maxArgs: 1
Save the following rule in rules/eligibility.json. It combines creditScore
with core comparisons and arithmetic:
{
"and": [
{">=": [{"creditScore": [{"var": "applicant.id"}]}, 700]},
{"<=": [
{"/": [
{"var": "monthlyDebtPayments"},
{"var": "grossMonthlyIncome"}
]},
0.35
]}
]
}
The caller supplies the applicant ID, monthly debt payments, and gross monthly
income. During evaluation, creditScore fetches the score from the host system.
Gramson checks the rule's structure before you save or run it, catching
unknown operators, missing arguments, and malformed nested expressions. For
example, it rejects {"creditScore": []} (missing an applicant) and
{"creditScore": [{"vra": "applicant.id"}]} (a misspelled nested operator).
Add more host operators and regenerate; the validation schema stays in sync.
Your application evaluates the rule, implements the lookup, and handles runtime checks such as nonzero income. Gramson generates the grammar.
Create a project folder with this structure. Save the declaration above in
language/custom/credit.yaml and the JSON rule in rules/eligibility.json:
loan-example/
├── language/
│ ├── language.yaml
│ └── custom/
│ └── credit.yaml
└── rules/
└── eligibility.json
Put this manifest in language/language.yaml:
include:
- gramson:core
operatorDirectories:
- custom
From inside loan-example/, generate the combined grammar, then validate your rule:
gramson generate --manifest language/language.yaml --output-directory generated
gramson validate rules/eligibility.json --schema generated/expression.schema.yaml
The validation command prints rules/eligibility.json: valid expression.
Generated output includes core and custom schemas, shared definitions, and one
expression entry point. Keep the directory together when moving it; validation
uses these files offline, without needing the source declarations.
Compose operator libraries
Select bundled extensions in your manifest:
include:
- gramson:core
- gramson:bitwise
The bundled bitwise group adds &, |, ^, ~, <<, >>, hexToInt, and
intToHex. Add operatorDirectories to combine these with local declarations.
Include only gramson:bitwise to select it without core.
For a user-owned library, include its namespace and group:
include:
- gramson:core
- mystuff:myops
gramson generate --manifest language.yaml --spec-path ./vendor --output-directory generated
This reads declarations from vendor/mystuff/myops/ recursively. Repeat
--spec-path for multiple search roots. Generated schemas validate without the
source library. See the
external library example.
Without include, a manifest selects only local directories. Include order gives
no precedence: duplicate operator keys, definition names, or output paths fail
generation. Pin the Gramson version for reproducible bundled declarations.
Input formats and exit codes
Files ending in .yaml or .yml are read as YAML; other files and stdin default
to JSON. Use --format json or --format yaml to override detection. Each input
contains one expression, without an API request envelope. YAML values must be
representable as JSON: duplicate or non-string keys, non-finite numbers,
timestamps, and cyclic aliases are rejected. Quote dates to use them as strings.
Exit codes are 0 for valid syntax, 1 for an invalid expression, and 2 for
input, schema, or command usage errors. python -m gramson validate ... is also
supported. See the
usage guide for details.
Compatibility and contents
The included JSON Logic syntax profile covers all 34 documented operators plus
the ?: alias. All 278 expressions in the pinned upstream corpus pass syntax
validation. Gramson applies stricter argument counts and object-shape rules than
the reference interpreter and validates every branch. There is no evaluator;
runtime equivalence is not tested.
The wheel includes the generated schemas under gramson.schemas, preserving
relative references for offline use. They can be accessed with
importlib.resources.files("gramson.schemas"). Core and optional extension declarations are bundled
under gramson.specs, and the generator is included in the installed package.
The source distribution also contains tests and user documentation.
See the project overview, operator specification, and issue tracker.
License and acknowledgements
Gramson is licensed under MIT. Its included expression language builds on JSON Logic, created by Jeremy Wadhams. The vendored upstream tests retain their original MIT license and attribution; see the third-party notices.
Metadata
Release files for gramson 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gramson-0.1.1.tar.gz | 65.9 kB | With provenance | Details for gramson-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gramson-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for gramson-0.1.1-py3-none-any.whl |
Total release size: 93.6 kB
Release files / gramson-0.1.1.tar.gz
| Download URL | gramson-0.1.1.tar.gz |
|---|---|
| Size | 65.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
71fdbceed87df5220697be2ca0ca0ba64de45450da2f67e057888211b22d6e4b
|
|
BLAKE2b-256 checksum How to use checksums |
b9575898094ca87059f1d6ef3b2405a582bae1a3ccd09d62cd29bbfdf033b3da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logRelease files / gramson-0.1.1-py3-none-any.whl
| Download URL | gramson-0.1.1-py3-none-any.whl |
|---|---|
| Size | 27.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12aea3a29d6c1811af45ec9acaf2b96986f60bf3580240f938a810c25f89d2d5
|
|
BLAKE2b-256 checksum How to use checksums |
438af24373764f8949868b63cb3362e568a547835c488593a3a9b3ee1e288f5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logSecurity for gramson 0.1.1
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for gramson-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logA 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.