@raphaellcs/json-converter
v1.0.0
Published
Convert between JSON, YAML, XML, and other formats
Maintainers
Readme
@raphaellcs/json-converter
A simple and powerful tool to convert between JSON, YAML, XML, and CSV formats.
Features
- 🔄 Convert JSON ↔ YAML
- 📄 Convert JSON ↔ XML
- 📊 Convert JSON ↔ CSV
- 🎯 Support for complex nested structures
- ⚡ Fast and lightweight
- 🎨 Pretty print option for readability
Installation
# Use with npx (no installation needed)
npx @raphaellcs/json-converter --help
# Or install globally
npm install -g @raphaellcs/json-converterUsage
JSON to YAML
# Convert JSON to YAML
npx @raphaellcs/json-converter json2yaml -i data.json
# Specify output file
npx @raphaellcs/json-converter json2yaml -i data.json -o data.yaml
# Compact output (no pretty print)
npx @raphaellcs/json-converter json2yaml -i data.json --no-prettyYAML to JSON
# Convert YAML to JSON
npx @raphaellcs/json-converter yaml2json -i config.yaml
# Specify output file
npx @raphaellcs/json-converter yaml2json -i config.yaml -o config.json
# Compact output
npx @raphaellcs/json-converter yaml2json -i config.yaml --no-prettyJSON to XML
# Convert JSON to XML
npx @raphaellcs/json-converter json2xml -i data.json
# Specify root element name
npx @raphaellcs/json-converter json2xml -i data.json -r document
# Specify output file
npx @raphaellcs/json-converter json2xml -i data.json -o data.xmlXML to JSON
# Convert XML to JSON
npx @raphaellcs/json-converter xml2json -i data.xml
# Specify output file
npx @raphaellcs/json-converter xml2json -i data.xml -o data.json
# Compact output
npx @raphaellcs/json-converter xml2json -i data.xml --no-prettyJSON to CSV
# Convert JSON array to CSV
npx @raphaellcs/json-converter json2csv -i data.json
# Specify output file
npx @raphaellcs/json-converter json2csv -i data.json -o data.csvCSV to JSON
# Convert CSV to JSON
npx @raphaellcs/json-converter csv2json -i data.csv
# Specify output file
npx @raphaellcs/json-converter csv2json -i data.csv -o data.json
# Compact output
npx @raphaellcs/json-converter csv2json -i data.csv --no-prettyExamples
Example 1: JSON to YAML
Input file (data.json):
{
"name": "John",
"age": 30,
"city": "New York",
"skills": ["JavaScript", "Python"]
}Command:
$ npx @raphaellcs/json-converter json2yaml -i data.json
✓ Converted successfully!
Output: data.yamlOutput file (data.yaml):
name: John
age: 30
city: New York
skills:
- JavaScript
- PythonExample 2: YAML to JSON
Input file (config.yaml):
server:
host: localhost
port: 3000
database:
name: myapp
user: postgresCommand:
$ npx @raphaellcs/json-converter yaml2json -i config.yaml -o config.json
✓ Converted successfully!
Output: config.jsonOutput file (config.json):
{
"server": {
"host": "localhost",
"port": 3000
},
"database": {
"name": "myapp",
"user": "postgres"
}
}Example 3: JSON to XML
Input file (data.json):
{
"user": {
"name": "Alice",
"email": "[email protected]"
}
}Command:
$ npx @raphaellcs/json-converter json2xml -i data.json -r document -o data.xml
✓ Converted successfully!
Output: data.xmlOutput file (data.xml):
<document>
<user>
<name>Alice</name>
<email>[email protected]</email>
</user>
</document>Example 4: JSON Array to CSV
Input file (users.json):
[
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Jane",
"age": 25,
"city": "London"
}
]Command:
$ npx @raphaellcs/json-converter json2csv -i users.json
✓ Converted successfully!
Output: users.csvOutput file (users.csv):
name,age,city
John,30,New York
Jane,25,LondonExample 5: CSV to JSON
Input file (products.csv):
name,price,stock
Laptop,999.99,50
Phone,699.99,100
Tablet,349.99,75Command:
$ npx @raphaellcs/json-converter csv2json -i products.json -o products.json
✓ Converted successfully!
Output: products.jsonOutput file (products.json):
[
{
"name": "Laptop",
"price": "999.99",
"stock": "50"
},
{
"name": "Phone",
"price": "699.99",
"stock": "100"
},
{
"name": "Tablet",
"price": "349.99",
"stock": "75"
}
]Options
Common Options
-i, --input <path>- Input file path (required)-o, --output <path>- Output file path (optional, auto-generates if not specified)-p, --pretty- Pretty print output (default: true)--no-pretty- Compact output (no pretty print)
JSON to XML Options
-r, --root <name>- Root element name (default: 'root')
Use Cases
- 📄 Configuration: Convert between JSON and YAML for config files
- 📊 Data Export: Convert database results to CSV
- 🔄 API Integration: Convert between API response formats
- 📦 Data Migration: Transform data between different systems
- 📝 Documentation: Convert JSON configs to human-readable YAML
- 🎯 Testing: Generate test data in different formats
Format Support
| From / To | JSON | YAML | XML | CSV | |-----------|------|------|-----|-----| | JSON | - | ✅ | ✅ | ✅ | | YAML | ✅ | - | ❌ | ❌ | | XML | ✅ | ❌ | - | ❌ | | CSV | ✅ | ❌ | ❌ | - |
Tips
1. Working with Nested Structures
{
"users": [
{ "name": "John", "age": 30 },
{ "name": "Jane", "age": 25 }
]
}When converting to CSV, the tool automatically extracts the array:
name,age
John,30
Jane,252. Custom Root Element for XML
npx @raphaellcs/json-converter json2xml -i data.json -r documentThis wraps your JSON in a <document> root element.
3. Pretty vs Compact Output
# Pretty (default, readable)
npx @raphaellcs/json-converter json2yaml -i data.json
# Compact (minified)
npx @raphaellcs/json-converter json2yaml -i data.json --no-pretty4. Auto-naming Output Files
If you don't specify output, the tool automatically names it:
data.json→data.yaml(json2yaml)config.yaml→config.json(yaml2json)data.json→data.xml(json2xml)data.csv→data.json(csv2json)
Limitations
- CSV conversion works best with flat data structures
- XML attributes are preserved but may appear as nested properties
- Very large files may use significant memory
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
License
MIT
Author
Dream Heart 🌙
