JSON Converter is a PHP library that provides advanced functionality to convert data between JSON, CSV, and XML formats.
- Convert
JSON
data toCSV
format. - Convert
JSON
data toXML
format. - Convert
CSV
data toJSON
format. - Convert
XML
data toJSON
format. - Support for custom delimiters and enclosures in CSV files.
- Pretty print option for XML output.
You can install JSON Converter library via Composer:
composer require ramazancetinkaya/json-converter
// Include the Composer autoloader
require 'vendor/autoload.php';
use JSONConverter\JSONConverter;
Let's create a JSON data to use for this example:
$jsonData = '[
{
"id": 1,
"name": "John Doe",
"position": "Software Engineer",
"department": "Engineering",
"salary": 75000
},
{
"id": 2,
"name": "Jane Smith",
"position": "Marketing Manager",
"department": "Marketing",
"salary": 65000
},
{
"id": 3,
"name": "Michael Johnson",
"position": "HR Specialist",
"department": "Human Resources",
"salary": 55000
}
]';
Files used for this example:
// CSV file
$csvFilePath = 'employees.csv';
// XML file
$xmlFilePath = 'employees.xml';
Convert JSON to CSV:
// Convert JSON to CSV
JSONConverter::jsonToCSV($jsonData, 'output.csv');
Convert JSON to XML:
// Convert JSON to XML
JSONConverter::jsonToXML($jsonData, 'output.xml', true);
Convert CSV to JSON:
// Convert CSV to JSON
$jsonFromCSV = JSONConverter::csvToJSON($csvFilePath);
echo $jsonFromCSV;
Convert XML to JSON:
// Convert XML to JSON
$jsonFromXML = JSONConverter::xmlToJSON($xmlFilePath);
echo $jsonFromXML;
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request.
This project is licensed under the MIT License. For more details, see the LICENSE file.
© 2024 Ramazan Çetinkaya. All rights reserved.