JSON to TOML 변환이란?
JSON to TOML 변환은 JavaScript Object Notation 데이터를 Tom's Obvious Minimal Language로 변환하는 작업입니다. JSON은 중괄호, 대괄호, 따옴표로 감싼 키를 사용해 구조화된 데이터를 표현합니다. TOML은 섹션 헤더(테이블)와 함께 평면적인 키-값 문법을 사용하며, INI 파일처럼 읽히지만 엄격한 타입 시스템을 갖춥니다. TOML은 기계 간 데이터 교환보다 사람이 읽기 쉬운 설정 파일에 최적화된 언어입니다.
TOML은 Rust(Cargo.toml), Python 패키징(pyproject.toml), Hugo 정적 사이트, Netlify 배포 설정, 다양한 CLI 도구의 기본 설정 형식으로 자리 잡았습니다. API 응답, 내보낸 설정, 생성된 파일 등 소스 데이터가 JSON이고 대상 시스템이 TOML을 요구할 때, JSON 객체를 TOML 테이블로, JSON 배열을 TOML 배열로 정확하게 매핑하는 변환기가 필요합니다.
온라인으로 JSON을 TOML로 변환하는 것은 기존 JSON 데이터에서 유효한 TOML을 생성하는 가장 빠른 방법입니다. 타입 매핑은 자동으로 처리됩니다. JSON 문자열은 TOML 문자열이 되고, JSON 숫자는 TOML 정수 또는 부동소수점이 되며, JSON 불리언은 그대로 매핑됩니다. JSON 객체는 TOML 테이블이 됩니다. 예외는 null입니다. TOML에는 null 타입이 없으므로, null 값은 변환기에 따라 키가 생략되거나 빈 문자열로 처리됩니다.
이 JSON to TOML 변환기를 사용하는 이유
TOML은 설정 파일이 요구하는 형식이고, JSON은 API와 도구가 생성하는 형식입니다. 이 변환기는 수동으로 다시 작성하지 않고 두 형식 사이를 오갈 수 있게 해 줍니다.
JSON to TOML 활용 사례
JSON vs TOML 비교
JSON과 TOML은 표현 능력이 겹치지만 문법, 타입 지원, 사용 목적에서 차이가 있습니다. 이 표는 변환에 영향을 미치는 주요 차이점을 보여 줍니다.
| 항목 | JSON | TOML |
|---|---|---|
| Syntax | Braces, brackets, colons, commas | Key = value, [table], [[array]] |
| Comments | Not allowed (RFC 8259) | Supported with # |
| Data types | string, number, boolean, null, object, array | string, integer, float, boolean, datetime, array, table |
| null support | Native (null) | No null type — omit the key or use empty string |
| Nested objects | Unlimited nesting depth | Dotted keys or [table.subtable] headers |
| Arrays of objects | Array of objects with [] | [[array-of-tables]] syntax |
| Readability | Moderate — bracket-heavy at depth | High — flat key-value pairs |
| Spec | RFC 8259 / ECMA-404 | TOML v1.0.0 (toml.io) |
TOML 변환 시 주의 사항
TOML에는 JSON과 다른 규칙이 있어 변환 결과에 영향을 줄 수 있습니다. 다음 네 가지 문제가 가장 많은 혼란을 일으킵니다.
코드 예제
프로그래밍 방식으로 JSON을 TOML로 변환하려면 대부분의 언어에서 TOML 직렬화 라이브러리가 필요합니다. JSON 파싱은 표준 라이브러리로 가능하지만 TOML 출력은 별도의 패키지가 필요합니다.
import { stringify } from '@iarna/toml'
const json = '{"title":"My App","database":{"host":"localhost","port":5432}}'
const obj = JSON.parse(json)
const toml = stringify(obj)
console.log(toml)
// → title = "My App"
// →
// → [database]
// → host = "localhost"
// → port = 5432import json
import tomli_w # pip install tomli_w
json_str = '{"title": "My App", "database": {"host": "localhost", "port": 5432}}'
data = json.loads(json_str)
toml_str = tomli_w.dumps(data)
print(toml_str)
# → title = "My App"
# →
# → [database]
# → host = "localhost"
# → port = 5432package main
import (
"encoding/json"
"fmt"
"github.com/pelletier/go-toml/v2"
)
func main() {
jsonStr := `{"title":"My App","database":{"host":"localhost","port":5432}}`
var data map[string]interface{}
json.Unmarshal([]byte(jsonStr), &data)
tomlBytes, _ := toml.Marshal(data)
fmt.Println(string(tomlBytes))
// → title = 'My App'
// →
// → [database]
// → host = 'localhost'
// → port = 5432
}# Using yj (https://github.com/sclevine/yj)
echo '{"title":"My App","port":3000}' | yj -jt
# → title = "My App"
# → port = 3000
# Using remarshal (pip install remarshal)
echo '{"title":"My App","port":3000}' | remarshal -if json -of toml
# → title = "My App"
# → port = 3000