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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
json2pystmt-0.4.0.tar.gz
(7.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json2pystmt-0.4.0.tar.gz.
File metadata
- Download URL: json2pystmt-0.4.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e9698263be8544233c8a2a00c0f1bedee290d2b0a7ede85c2fc312b760b21b1
|
|
| MD5 |
40d2ce149838ee37ce2280756437cc1a
|
|
| BLAKE2b-256 |
068773c5e7258dd036850837b907fad357d6fb42cb9f0f164de41eb5627a6c96
|
File details
Details for the file json2pystmt-0.4.0-py3-none-any.whl.
File metadata
- Download URL: json2pystmt-0.4.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e00fe44769c5a9848a8d8802273f34b599a5a5f953d619d02920ef08e162df6
|
|
| MD5 |
4ad36f585f2810d0cc89ab2230376620
|
|
| BLAKE2b-256 |
b5fc8e8b16f320cfbab2b66a3c945dcd4c8d5dd3d2f75efc7c00359b0b6a71f9
|