Convert JSON to Python assignment statements. Useful for grepping JSON, diffing JSON objects, and debugging.
Project description
json2pystmt
Convert JSON to Python assignment statements. Useful for grepping JSON, diffing JSON objects, and debugging.
Installation
pip install json2pystmt
Or with uv:
uv add json2pystmt
Command Line Usage
# From file
json2pystmt data.json
# From stdin
cat data.json | json2pystmt
# Custom root variable name
json2pystmt -r myvar data.json
Example
Input JSON:
{"key1": [1, 2, {"x": {"y": "z"}}]}
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
json2pystmt(jsonobj, rootname="root")
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")- Returns: List of assignment statement strings
Requirements
- Python 3.10+
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
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.2.0.tar.gz.
File metadata
- Download URL: json2pystmt-0.2.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c984e283f775a1c6e5f008e28faacd7136499b2fa880aa3e5d9a160657d5b09
|
|
| MD5 |
061edd5be454ca7f9f82f48ebb32d1a5
|
|
| BLAKE2b-256 |
520c9eec8ab99ebfa279e64c4b72a1afc307a60be0453a11fa2beed2a7857b18
|
File details
Details for the file json2pystmt-0.2.0-py3-none-any.whl.
File metadata
- Download URL: json2pystmt-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.2 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 |
4100632340f45851003402cd75cc041488738a6aff0fc010e53e2e77bcf4795b
|
|
| MD5 |
bcce3afdee6429bb607ad3a42954b05d
|
|
| BLAKE2b-256 |
3f8700f606a3f5acde68538f004fb29af1898928dac9a5dab4bc92917b0c6615
|