Skip to main content

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 length
  • max_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)

Source distribution for json2pystmt 0.4.0
File Size Uploaded
json2pystmt-0.4.0.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json2pystmt 0.4.0
File Interpreter ABI Platform
json2pystmt-0.4.0-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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