SQL 결과 → JSON 변환기
SQL 쿼리 결과(MySQL/PostgreSQL CLI 출력)를 JSON으로 변환합니다. 탭 및 파이프 구분 형식을 지원합니다.
이 도구 소개
SQL to JSON Converter는 MySQL이나 PostgreSQL이 터미널에 출력하는 테이블을 깔끔한 JSON 배열로 변환합니다. 콘솔 쿼리 결과를 일일이 다시 입력하는 대신, API나 픽스처, 스크립트에 바로 넣을 수 있는 구조화된 데이터를 얻을 수 있습니다.
CLI 결과 테이블을 붙여 넣으면 각 행이 열 이름을 키로 하는 객체가 된 JSON 배열을 돌려줍니다. 테스트 데이터 시딩, 빠른 API 모킹, 데이터베이스 클라이언트 없이 쿼리 결과를 코드로 옮기는 데 유용합니다.
탭 구분과 파이프 구분 테이블 출력을 모두 지원하므로 MySQL과 PostgreSQL CLI 형식이 둘 다 동작합니다. 모든 처리가 브라우저에서 로컬로 이루어지므로 쿼리 데이터가 어디로도 업로드되지 않습니다.
자주 묻는 질문
코드 구현
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.