🛠️ToolsShed

INI to JSON

INI 설정 파일을 JSON으로 변환하고 역방향도 지원합니다.

자주 묻는 질문

코드 구현

import configparser
import json

def ini_to_json(ini_text: str) -> dict:
    config = configparser.ConfigParser()
    config.read_string(ini_text)
    result = {}
    for section in config.sections():
        result[section] = dict(config[section])
    # Top-level keys (DEFAULT section workaround)
    for key, value in config.defaults().items():
        result[key] = value
    return result

ini_text = """
[database]
host = localhost
port = 5432
name = mydb
"""

data = ini_to_json(ini_text)
print(json.dumps(data, indent=2))

Comments & Feedback

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