JSONからTOMLへの変換とは?
JSONからTOMLへの変換は、JavaScript Object Notation(JSON)のデータをTom's Obvious Minimal Language(TOML)に変換する処理です。JSONは波括弧・角括弧・引用符で囲まれたキーを使って構造化データを表現します。TOMLはセクションヘッダー(テーブルと呼ばれる)を持つフラットなキーと値の構文を使用し、INIファイルに近い読みやすさを持ちながら厳格な型付けをサポートします。TOMLはマシン間のデータ交換よりも人間による可読性を重視した設定ファイル向けに設計されました。
TOMLはRust(Cargo.toml)、Pythonパッケージング(pyproject.toml)、Hugoスタティックサイト、Netlifyデプロイ設定、その他多くのCLIツールのデフォルト設定フォーマットとなっています。ソースデータがJSONとして存在し(APIレスポンス、エクスポートされた設定、生成されたファイルなど)、ターゲットシステムが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と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と異なるルールがあり、変換結果に影響します。以下の4つの問題が最も混乱を招きやすいものです。
コード例
プログラムで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