コンテンツへスキップ
🛠️ToolsShed

テキスト → JSON 変換ツール

テキストの行をJSON配列・オブジェクトに変換、またはキー:値ペアを解析します。複数の変換モードに対応。

このツールについて

テキストから JSON への変換ツールは、構造化されていないテキストを有効な JSON 形式に変換し、プレーンテキストデータを機械読取可能な構造に簡単に変換できます。設定ファイル、API データ、または単純なリストを操作している場合でも、このツールは JSON 構文を手動で記述する必要がなく、テキストベースの情報をアプリケーションと API が直接消費できる形式に迅速に移行するのに役立ちます。

このコンバーターを使用するには、テキストを貼り付けて、データ構造に一致する変換モードを選択するだけです。このツールは、カンマ区切りアイテムの配列形式、キーと値のペア(コロンまたは等号区切り記号を使用)のオブジェクト表記法、および各行が配列要素になる行ごとの変換をサポートしています。適切なモードを選択して変換をクリックすると、正しくフォーマットされた JSON が生成され、プロジェクトで直ちに使用できます。

このツールは、レガシーテキストベースデータを移行する開発者、テストのために JSON フィクスチャを作成する場合、または構文を手動で記述せずに設定構造を迅速にプロトタイプ化する場合に非常に価値があります。コンテンツ作成者、アナリスト、および構造化データを操作している人は、誰でも反復的な JSON フォーマット作業を排除することでワークフローを加速させることができます。

よくある質問

コード実装

# Text to JSON Conversion Utilities
import json

def lines_to_array(text: str, skip_empty: bool = True, trim: bool = True) -> list:
    lines = text.split("\n")
    if trim:
        lines = [l.strip() for l in lines]
    if skip_empty:
        lines = [l for l in lines if l]
    return lines

def lines_to_objects(text: str, key: str = "text", skip_empty: bool = True) -> list:
    lines = lines_to_array(text, skip_empty)
    return [{key: line} for line in lines]

def keyvalue_to_object(text: str, trim: bool = True) -> dict:
    result = {}
    for line in text.split("\n"):
        if trim:
            line = line.strip()
        for sep in [":", "="]:
            idx = line.find(sep)
            if idx > 0:
                k = line[:idx].strip()
                v = line[idx+1:].strip()
                result[k] = v
                break
    return result

def csv_to_json(text: str, trim: bool = True) -> list:
    lines = text.split("\n")
    if not lines:
        return []
    headers = [h.strip() if trim else h for h in lines[0].split(",")]
    result = []
    for line in lines[1:]:
        if not line.strip():
            continue
        values = [v.strip() if trim else v for v in line.split(",")]
        result.append({headers[i]: values[i] if i < len(values) else "" for i in range(len(headers))})
    return result

# Examples
text = "apple\nbanana\ncherry"
print(json.dumps(lines_to_array(text), indent=2))

kv_text = "name: Alice\nage: 30\ncity: London"
print(json.dumps(keyvalue_to_object(kv_text), indent=2))

csv_text = "name,age,city\nAlice,30,London\nBob,25,Paris"
print(json.dumps(csv_to_json(csv_text), indent=2))

Comments & Feedback

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