| name | data-type-converter |
| description | Convert between data formats (JSON, CSV, XML, YAML, TOML). Handles nested structures, arrays, and preserves data types where possible. |
Data Type Converter
Convert data between JSON, CSV, XML, YAML, and TOML formats. Handles nested structures, arrays, and complex data with intelligent flattening options.
Quick Start
from scripts.data_converter import DataTypeConverter
converter = DataTypeConverter()
converter.convert("data.json", "data.csv")
converter.convert("config.yaml", "config.json")
converter.convert("data.json", "data.csv", flatten=True)
Features
- 5 Formats: JSON, CSV, XML, YAML, TOML
- Nested Data: Flatten or preserve nested structures
- Arrays: Handle array data intelligently
- Type Preservation: Maintain data types where possible
- Pretty Output: Formatted, human-readable output
- Batch Processing: Convert multiple files
API Reference
Basic Conversion
converter = DataTypeConverter()
converter.convert("input.json", "output.csv")
converter.convert("input.xml", "output.json")
converter.convert("input.yaml", "output.toml")
With Options
converter.convert("nested.json", "flat.csv", flatten=True)
converter.convert("data.json", "pretty.json", indent=4)
converter.convert("data.json", "data.xml", root="records")
Programmatic Access
data = converter.load("data.json")
converter.save(data, "data.yaml")
json_str = '{"name": "John", "age": 30}'
yaml_str = converter.convert_string(json_str, "json", "yaml")
Batch Processing
converter.batch_convert(
input_dir="./json_files",
output_dir="./csv_files",
output_format="csv"
)
CLI Usage
python data_converter.py --input data.json --output data.csv
python data_converter.py --input nested.json --output flat.csv --flatten
python data_converter.py --input-dir ./json --output-dir ./csv --format csv
python data_converter.py --input data.json --output pretty.json --indent 4
CLI Arguments
| Argument | Description | Default |
|---|
--input | Input file | Required |
--output | Output file | Required |
--input-dir | Input directory for batch | - |
--output-dir | Output directory | - |
--format | Output format | From extension |
--flatten | Flatten nested data | False |
--indent | Indentation spaces | 2 |
--root | XML root element | root |
Conversion Matrix
| From/To | JSON | CSV | XML | YAML | TOML |
|---|
| JSON | - | Yes | Yes | Yes | Yes |
| CSV | Yes | - | Yes | Yes | Yes |
| XML | Yes | Yes | - | Yes | Yes |
| YAML | Yes | Yes | Yes | - | Yes |
| TOML | Yes | Yes | Yes | Yes | - |
Examples
JSON to CSV (Flat)
converter = DataTypeConverter()
converter.convert("data.json", "data.csv")
Nested JSON to Flat CSV
converter.convert("nested.json", "flat.csv", flatten=True)
YAML Config to JSON
converter.convert("config.yaml", "config.json")
XML to JSON
converter.convert("data.xml", "data.json")
Dependencies
pyyaml>=6.0
toml>=0.10.0
xmltodict>=0.13.0
pandas>=2.0.0
Limitations
- CSV doesn't support nested data (requires flattening)
- XML attribute handling is basic
- TOML doesn't support null values
- Very deep nesting may cause issues with some formats
- Array handling varies by format