SQL-Ergebnis zu JSON Konverter
SQL-Abfrageergebnisse (MySQL/PostgreSQL CLI-Ausgabe) in JSON konvertieren. Unterstützt tabulator- und pipe-getrennte Formate.
Häufig gestellte Fragen
Code-Implementierung
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.