Introduction
ast2json is a small module that will convert any python AST node into its ast representation formatted in a JSON compatible python representation (list and dicts containing string, ints and null).
Installation
pip install ast2json # or to have lastest version pip install git+git://github.com/Psycojoker/ast2json.git
Usage
>>> import json
>>> from ast import parse
>>> from ast2json import ast2json
>>> ast = ast2json(parse(open('some_python_source_file.py').read()))
>>> print json.dumps(ast, indent=4)
If you are lazy, “str2json” will apply the “parse” method of ast on a string for you, so you’ll be able to write:
>>> str2json(open('some_python_source_file.py').read())
You can also run via the command line to parse Python source from stdin and print to stdout:
$ ast2json < some_python_source_file.py
Example
This is the result of converting ‘print “Hello World!”’ (and applying json.dumps on the result).
{
"body": [
{
"_type": "Print",
"nl": true,
"col_offset": 0,
"dest": null,
"values": [
{
"s": "Hello World!",
"_type": "Str",
"lineno": 1,
"col_offset": 6
}
],
"lineno": 1
}
],
"_type": "Module"
}
Changelog
0.4 (2025-02-07)
you can now use “ast2json” directly from the CLI (it reads stdin) thx to @vergenzt
output keys are now sorted to be deterministic
various small code improvements
0.2.1 (2016-11-13)
fix broken pip installation by @abolger
0.2 (2016-09-03)
python 3 support and some unit testing by Juncheol Cho @zironycho
Licence
BSD
Metadata
Release files for ast2json 0.4
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 |
|---|---|---|---|---|
| ast2json-0.4.tar.gz | 4.7 kB | Without provenance | Details for ast2json-0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ast2json-0.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for ast2json-0.4-py2.py3-none-any.whl |
Total release size: 10.2 kB
Release files / ast2json-0.4.tar.gz
| Download URL | ast2json-0.4.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5cc42e97c29b77845b5d4ec32e74dcac538fb4c61a7faa570964bc96b19aeb7
|
|
BLAKE2b-256 checksum How to use checksums |
2bdc7c11f80a782f051beafcc4707dd54888e7fcdeea5afa9236aa2e2cfa7073
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Release files / ast2json-0.4-py2.py3-none-any.whl
| Download URL | ast2json-0.4-py2.py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
df60746b65384a7fdfc7ef29a6f703ef9cc6e643586e7636e0a44a9f47901810
|
|
BLAKE2b-256 checksum How to use checksums |
f0efcc0efc91e4f0f98780ca77f073578f9b7e5e687c5577bdc5537a3b2d7bf3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Security for ast2json 0.4
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.