Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export parsed structure to JSON #42

Open
ispmarin opened this issue Nov 20, 2018 · 1 comment
Open

Export parsed structure to JSON #42

ispmarin opened this issue Nov 20, 2018 · 1 comment

Comments

@ispmarin
Copy link

Is it possible to export the parsed structure to JSON, similar to pglast does?

@akeeman
Copy link
Contributor

akeeman commented Nov 22, 2018

Source:

import json
import psqlparse

query = "SELECT * FROM my_table"

json_str = json.dumps(psqlparse.parse_dict(query), sort_keys=True, indent=4)
print(json_str)

Result:

[
    {
        "SelectStmt": {
            "fromClause": [
                {
                    "RangeVar": {
                        "inhOpt": 2,
                        "location": 14,
                        "relname": "my_table",
                        "relpersistence": "p"
                    }
                }
            ],
            "op": 0,
            "targetList": [
                {
                    "ResTarget": {
                        "location": 7,
                        "val": {
                            "ColumnRef": {
                                "fields": [
                                    {
                                        "A_Star": {}
                                    }
                                ],
                                "location": 7
                            }
                        }
                    }
                }
            ]
        }
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants