Skip to main content

convert a python source code into json-dumpable data (dict and lists with strings, ints, ...)

Project description

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

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

ast2json-0.4.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

ast2json-0.4-py2.py3-none-any.whl (5.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ast2json-0.4.tar.gz.

File metadata

  • Download URL: ast2json-0.4.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for ast2json-0.4.tar.gz
Algorithm Hash digest
SHA256 b5cc42e97c29b77845b5d4ec32e74dcac538fb4c61a7faa570964bc96b19aeb7
MD5 12ee61e47e94b065b61113750ccfe9ab
BLAKE2b-256 2bdc7c11f80a782f051beafcc4707dd54888e7fcdeea5afa9236aa2e2cfa7073

See more details on using hashes here.

File details

Details for the file ast2json-0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: ast2json-0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for ast2json-0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 df60746b65384a7fdfc7ef29a6f703ef9cc6e643586e7636e0a44a9f47901810
MD5 36cd5df97b96fc152d99134c7aff9c07
BLAKE2b-256 f0efcc0efc91e4f0f98780ca77f073578f9b7e5e687c5577bdc5537a3b2d7bf3

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