ramazancetinkaya/json-converter

This library provides methods for converting JSON data to CSV, XML, and vice versa.

dev-main 2024-05-14 19:46 UTC

This package is auto-updated.

Last update: 2024-10-15 12:28:15 UTC


README

License GitHub issues GitHub stars GitHub forks

JSON Converter is a PHP library that provides advanced functionality to convert data between JSON, CSV, and XML formats.

Features

  • Convert JSON data to CSV format.
  • Convert JSON data to XML format.
  • Convert CSV data to JSON format.
  • Convert XML data to JSON format.
  • Support for custom delimiters and enclosures in CSV files.
  • Pretty print option for XML output.

Installation

You can install JSON Converter library via Composer:

composer require ramazancetinkaya/json-converter

Usage

// 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;

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request.

License

This project is licensed under the MIT License. For more details, see the LICENSE file.

Copyright

© 2024 Ramazan Çetinkaya. All rights reserved.