json2pystmt
Convert JSON to Python assignment statements. Useful for grepping JSON, diffing JSON objects, and debugging.
Installation
pip install json2pystmt
Or with uv:
uv tool install json2pystmt
Command Line Usage
❯ json2pystmt --help
usage: json2pystmt [-h] [-v] [-k MAX_KEY] [-m MAX_VALUE] [-r ROOT] [filename]
Convert JSON to executable Python statements
positional arguments:
filename JSON file to process (default: stdin)
options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-k, --max-key-length MAX_KEY
Maximum key length (default: -1)
-m, --max-value-length MAX_VALUE
Maximum key length (default: -1)
-r, --root ROOT Root variable name (default: root)
Example
Input JSON:
{"key1": [1, 2, {"x": {"y": "z"}}]}
# From file
json2pystmt data.json
# From stdin
cat data.json | json2pystmt
Output:
root = {}
root['key1'] = [None] * 3
root['key1'][0] = 1
root['key1'][1] = 2
root['key1'][2] = {}
root['key1'][2]['x'] = {}
root['key1'][2]['x']['y'] = 'z'
Use Cases
Grep for values in JSON:
json2pystmt data.json | grep "error"
Diff two JSON files:
diff <(json2pystmt a.json) <(json2pystmt b.json)
Library Usage
from json2pystmt import json2pystmt
data = {"users": [{"name": "Alice"}, {"name": "Bob"}]}
lines = json2pystmt(data)
for line in lines:
print(line)
Output:
root = {}
root['users'] = [None] * 2
root['users'][0] = {}
root['users'][0]['name'] = 'Alice'
root['users'][1] = {}
root['users'][1]['name'] = 'Bob'
API
def json2pystmt(jsonobj: Any, rootname: str = "root", max_key: int = -1, max_value: int = -1)
Convert a JSON-compatible object to a list of Python assignment statements.
jsonobj: Any JSON-compatible Python object (dict, list, str, int, float, bool, None)rootname: Variable name to use as the root (default:"root")max_key: Maximum key lengthmax_value: Maximum value length- Returns: List of assignment statement strings
Requirements
- Python 3.10+
License
MIT
Metadata
Release files for json2pystmt 0.4.0
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 |
|---|---|---|---|---|
| json2pystmt-0.4.0.tar.gz | 7.0 kB | Without provenance | Details for json2pystmt-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json2pystmt-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for json2pystmt-0.4.0-py3-none-any.whl |
Total release size: 12.1 kB
Release files / json2pystmt-0.4.0.tar.gz
| Download URL | json2pystmt-0.4.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e9698263be8544233c8a2a00c0f1bedee290d2b0a7ede85c2fc312b760b21b1
|
|
BLAKE2b-256 checksum How to use checksums |
068773c5e7258dd036850837b907fad357d6fb42cb9f0f164de41eb5627a6c96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / json2pystmt-0.4.0-py3-none-any.whl
| Download URL | json2pystmt-0.4.0-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e00fe44769c5a9848a8d8802273f34b599a5a5f953d619d02920ef08e162df6
|
|
BLAKE2b-256 checksum How to use checksums |
b5fc8e8b16f320cfbab2b66a3c945dcd4c8d5dd3d2f75efc7c00359b0b6a71f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Security for json2pystmt 0.4.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.