composer require pearl/csv-json-converter
$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;
- Array or Json values are accepted.
- Custom output header optional available. This is optional parameter if not passed then default header will be considered.
$jsonToCsv = new JsonToCsv($jsonString, ['headers' => ["productName", "author", "publisher", "lang"]]);
Or load the json data from file.
$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();
name | author | publisher | language |
---|---|---|---|
Half Girlfriend | Chetan Bhagat | Rupa Publications | en |
My Journey: Transforming Dreams into Actions | A.P.J. Abdul Kalam | Rupa Publications | en |
use Pearl\CsvJsonConverter\Type\CsvToJson;
Load the CSV data.
$csvToJson = new CsvToJson($csvString, ['bitmask' => 'JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES']);
Or Load the csv data from file.
$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();
name | author | publisher | language |
---|---|---|---|
Half Girlfriend | Chetan Bhagat | Rupa Publications | en |
My Journey: Transforming Dreams into Actions | A.P.J. Abdul Kalam | Rupa Publications | en |
[{
"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"
}
]