Skip to main content

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 tool install 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

json2pystmt-0.3.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json2pystmt-0.3.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file json2pystmt-0.3.0.tar.gz.

File metadata

  • Download URL: json2pystmt-0.3.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for json2pystmt-0.3.0.tar.gz
Algorithm Hash digest
SHA256 78eea0a03b21bec8ec583d11ecdbd6b6cf898d41cdb54bc5a4b19f341b51ead1
MD5 4e084e09846e99936b499f5882b9abd5
BLAKE2b-256 79995f31a72ba2f4a3346f0edd490de0f96161904aa00039c8ab0dec3352a540

See more details on using hashes here.

File details

Details for the file json2pystmt-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: json2pystmt-0.3.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

Hashes for json2pystmt-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c6ed3fe4ff0de7894659024f215804f13110da3c8fd5916d97fbaa8c8a55b00
MD5 897341210de219e655f00a814e91855a
BLAKE2b-256 2a6d660834fce434b96bee53c0dab3b4738343905ce2b10bface56298ccef571

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page