PHP code example of pearl / csv-json-converter

1. Go to this page and download the library: Download pearl/csv-json-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pearl / csv-json-converter example snippets


 $jsonString = '[{
	 "name": "Half Girlfriend",
	 "author": "Chetan Bhagat",
	 "publisher": "Rupa Publications",
	 "language": "en"
},
{
	 "name": "My Journey: Transforming Dreams into Actions",
	 "author": "A.P.J. Abdul Kalam",
	 "publisher": "Rupa Publications",
	 "language": "en"
}]';
 

use Pearl\CsvJsonConverter\Type\JsonToCsv;	

$jsonToCsv = new JsonToCsv($jsonString, ['headers' => ["productName", "author", "publisher", "lang"]]);

$jsonToCsv->load(__Dir__ . '/data/products.json');

<!-- Convert and save the result to specificed path -->
$jsonToCsv->convertAndSave(__Dir__ . '/output');

<!-- Convert and force download the file in browser-->
$jsonToCsv->convertAndDownload(__Dir__ . '/output');

<!-- Convert and get data-->
$jsonToCsv->convert();


use Pearl\CsvJsonConverter\Type\CsvToJson;

$csvToJson = new CsvToJson($csvString, ['bitmask' => 'JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES']);

$csvToJson->load(__Dir__ . '/data/products.csv');

<!-- Convert and save to specificed path -->
$csvToJson->convertAndSave(__Dir__ . '/output');

<!-- Convert and force download the file-->
$csvToJson->convertAndDownload(__Dir__ . '/output');

<!-- Convert and get data-->
$csvToJson->convert();