← 返回
未分类

Config Format Converter

Convert and validate configuration files between JSON, YAML, and TOML formats. Use when working with config files that need format conversion, syntax validat...
在不同配置文件格式(JSON、YAML、TOML)之间进行转换和校验。适用于需要对配置文件进行格式转换或语法校验的场景。
leonardodpanda
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 154
下载
💾 0
安装
1
版本
#latest

概述

Config Format Converter

Universal configuration file format converter for JSON, YAML, and TOML.

When to Use

  • Converting package.json to pyproject.toml or vice versa
  • Migrating CI/CD configs between formats (.yaml.json)
  • Normalizing config files for cross-platform projects
  • Validating syntax before committing config changes
  • Pretty-printing minified config files

Supported Formats

FormatExtensionsUse Cases
------------------------------
JSON.jsonnpm, Node.js, VS Code settings
YAML.yaml, .ymlDocker Compose, GitHub Actions, Kubernetes
TOML.tomlPython Poetry, Rust Cargo, Go modules

Quick Start

Convert Between Formats

import json
import yaml
import toml

# JSON to YAML
with open('package.json') as f:
    data = json.load(f)
with open('package.yaml', 'w') as f:
    yaml.dump(data, f, default_flow_style=False, sort_keys=False)

# YAML to JSON
with open('docker-compose.yaml') as f:
    data = yaml.safe_load(f)
with open('docker-compose.json', 'w') as f:
    json.dump(data, f, indent=2)

# TOML to JSON
with open('pyproject.toml') as f:
    data = toml.load(f)
with open('pyproject.json', 'w') as f:
    json.dump(data, f, indent=2)

Validate Config Syntax

import json
import yaml

def validate_json(filepath):
    try:
        with open(filepath) as f:
            json.load(f)
        return True, "Valid JSON"
    except json.JSONDecodeError as e:
        return False, str(e)

def validate_yaml(filepath):
    try:
        with open(filepath) as f:
            yaml.safe_load(f)
        return True, "Valid YAML"
    except yaml.YAMLError as e:
        return False, str(e)

Pretty-Print Configs

import json

# Compact to pretty JSON
with open('config.min.json') as f:
    data = json.load(f)
with open('config.json', 'w') as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

Common Workflows

Migrate npm Project to Python Poetry

import json
import toml

# Read package.json
with open('package.json') as f:
    pkg = json.load(f)

# Create pyproject.toml structure
pyproject = {
    'tool': {
        'poetry': {
            'name': pkg.get('name', ''),
            'version': pkg.get('version', '0.1.0'),
            'description': pkg.get('description', ''),
            'authors': [],
            'dependencies': {}
        }
    }
}

# Write TOML
with open('pyproject.toml', 'w') as f:
    toml.dump(pyproject, f)

Convert Docker Compose YAML to JSON for CI

import yaml
import json

with open('docker-compose.yaml') as f:
    compose = yaml.safe_load(f)

with open('docker-compose.json', 'w') as f:
    json.dump(compose, f, indent=2)

Best Practices

  • Preserve comments: YAML/TOML comments are lost in conversion; document important notes separately
  • Key ordering: Use sort_keys=False to preserve original key order
  • Unicode: Always use ensure_ascii=False for international configs
  • Validation: Always validate output after conversion
  • Backup: Keep original files before bulk conversion

Error Handling

Common issues and solutions:

IssueCauseFix
-------------------
ScannerErrorInvalid YAML syntaxCheck indentation (spaces, not tabs)
TomlDecodeErrorInvalid TOML syntaxVerify section headers [section]
JSONDecodeErrorTrailing commas in JSONRemove commas before } or ]
Unicode errorsEncoding mismatchOpen files with encoding='utf-8'

Dependencies

pip install pyyaml toml

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-12 06:15 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

security-compliance

SSL Certificate Checker

leonardodpanda
检查任意域名的SSL/TLS证书详情,包括到期日期、颁发者、有效性、加密套件和安全警告。用于验证HTTPS连接安全性。
★ 0 📥 798
productivity

QR Code Tool

leonardodpanda
为网址、文本、WiFi 凭证、名片等生成二维码。适用于为营销资料创建可扫描链接、分享 WiFi 密码等场景。
★ 0 📥 740
security-compliance

REST API Tester

leonardodpanda
测试REST API,支持自定义请求头、认证方式和请求体。用于调试API端点、测试认证流程、验证响应等场景。
★ 1 📥 1,124