Skip to content
πŸ› οΈToolsShed

SQL Result to JSON Converter

Convert SQL query results (MySQL/PostgreSQL CLI output) to JSON. Supports tab-separated and pipe-separated formats.

About this tool

SQL to JSON Converter turns the table that MySQL or PostgreSQL prints in your terminal into a clean JSON array. Instead of manually retyping console query results, you get structured data you can drop straight into APIs, fixtures, or scripts.

Paste the CLI result table and the tool returns a JSON array where each row becomes an object keyed by column name. It is handy for seeding test data, building quick API mocks, or moving query output into your code without a database client.

It accepts both tab-separated and pipe-separated table output, so MySQL and PostgreSQL CLI formats both work. Everything runs locally in your browser, so your query data is never uploaded anywhere.

Frequently Asked Questions

Code Implementation

import json
import re

def parse_mysql_output(text):
    """Parse MySQL CLI pipe-separated output to JSON."""
    lines = text.strip().splitlines()
    # Filter out separator lines (+---+) and empty lines
    data_lines = [l for l in lines if l.strip() and not re.match(r'^\s*[+\-]+', l)]
    if len(data_lines) < 2:
        return []

    # Split by pipe and strip whitespace
    def split_row(line):
        return [c.strip() for c in line.split('|') if c.strip() != '']

    headers = split_row(data_lines[0])
    rows = [split_row(l) for l in data_lines[1:]]

    return [dict(zip(headers, row)) for row in rows]

# Example: MySQL output
mysql_output = """
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | Alice |    95 |
|  2 | Bob   |    87 |
+----+-------+-------+
"""

result = parse_mysql_output(mysql_output)
print(json.dumps(result, indent=2))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.