SQL to JSON Converter
Transform SQL SELECT statements or query results into structured JSON data
Describe a SELECT statement as JSON
Sometimes you need a query's structure in a form a program can read: which table does it hit, which columns does it ask for, and what was the original text? SQL to JSON pulls those pieces out of a SELECT statement and returns them as a small JSON document you can drop into a config file, a test fixture or a documentation snippet.
How to convert a query
- Paste a SELECT statement into the SQL Query box. Sample loads a short example selecting four columns from users.
- Click Convert to JSON Structure.
- Read the JSON Representation panel and press Copy to take it.
For the sample query, the output has three fields: table, set to users; columns, an array with id, username, email and role; and query, holding the original SQL text with surrounding whitespace trimmed. The JSON is indented with four spaces.
What this tool is, and is not
It describes a query; it does not run it. No database is contacted, so you will not get rows of results back as JSON. If you want to convert actual data, export the result set from your database client instead. The extraction is done with pattern matching on the text between SELECT and the first FROM, and on the first word after FROM.
Practical uses
- Recording the column list of a report query in a metadata file.
- Feeding a simple query description into a script that builds documentation.
- Checking which columns a colleague's query returns, one item per line.
- Creating sample data for a front-end component that displays query definitions.
Cases where it will fail or mislead
The pattern expects the column list to sit on the same line as the word SELECT, so a query with one column per line produces the message that it could not parse the SQL structure; put the columns on one line first, or run the query through a minifier. Columns are split at every comma, which breaks function calls with several arguments such as COALESCE(a, b) into separate entries. Aliases stay attached to their column text. The table value is only the first word after FROM, so schema-qualified names like public.users return only the schema part, and joins, subqueries or multiple tables are not represented. Only SELECT statements are meant to be converted. Everything runs in your browser.
Related Tools
Related Tools