|
| 1 | +# JSON to CSV and CSV to JSON Converter |
| 2 | + |
| 3 | +[![Latest Version on Packagist][ico-version]][link-packagist] |
| 4 | +[![Software License][ico-license]](LICENSE.md) |
| 5 | +[![Build Status][ico-travis]][link-travis] |
| 6 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 7 | + |
| 8 | +The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies. |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +Via Composer |
| 13 | + |
| 14 | +``` bash |
| 15 | +$ composer require ozdemirburak/json-csv |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### JSON to CSV Converter |
| 21 | + |
| 22 | +``` php |
| 23 | +use OzdemirBurak\JsonCsv\File\Json; |
| 24 | + |
| 25 | +// JSON to CSV |
| 26 | +$json = new Json(__DIR__ . '/above.json'); |
| 27 | +// To convert JSON to CSV string |
| 28 | +$csvString = $json->convert(); |
| 29 | +// To convert JSON to CSV and save |
| 30 | +$json->convertAndSave(__DIR__ . '/above.csv'); |
| 31 | +// To convert JSON to CSV and force download on browser |
| 32 | +$json->convertAndDownload(); |
| 33 | +``` |
| 34 | + |
| 35 | +Assume that the input JSON is something like below. |
| 36 | + |
| 37 | +```json |
| 38 | +[ |
| 39 | + { |
| 40 | + "name": { |
| 41 | + "common": "Turkey", |
| 42 | + "official": "Republic of Turkey", |
| 43 | + "native": "T\u00fcrkiye" |
| 44 | + }, |
| 45 | + "area": 783562, |
| 46 | + "latlng": [39, 35] |
| 47 | + }, |
| 48 | + { |
| 49 | + "name": { |
| 50 | + "common": "Israel", |
| 51 | + "official": "State of Israel", |
| 52 | + "native": "\u05d9\u05e9\u05e8\u05d0\u05dc" |
| 53 | + }, |
| 54 | + "area": 20770, |
| 55 | + "latlng": [31.30, 34.45] |
| 56 | + } |
| 57 | +] |
| 58 | +``` |
| 59 | + |
| 60 | +After the conversion, the resulting CSV data will look like below. |
| 61 | + |
| 62 | +| name_common,name_official,name_native,area,latlng_0,latlng_1 | |
| 63 | +|--------------------------------------------------------------| |
| 64 | +| Turkey,"Republic of Turkey",Türkiye,783562,39,35 | |
| 65 | +| Israel,"State of Israel",ישראל,20770,31.3,34.45 | |
| 66 | + |
| 67 | + |
| 68 | +### CSV to JSON Converter |
| 69 | + |
| 70 | +``` php |
| 71 | +use OzdemirBurak\JsonCsv\File\Csv; |
| 72 | + |
| 73 | +// CSV to JSON |
| 74 | +$csv = new Csv(__DIR__ . '/below.csv'); |
| 75 | +$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); |
| 76 | +// To convert CSV to JSON string |
| 77 | +$jsonString = $csv->convert(); |
| 78 | +// To convert CSV to JSON and save |
| 79 | +$csv->convertAndSave(__DIR__ . '/below.csv'); |
| 80 | +// To convert CSV to JSON and force download on browser |
| 81 | +$csv->convertAndDownload(); |
| 82 | +``` |
| 83 | + |
| 84 | +Assume that the input CSV file is something like below. |
| 85 | + |
| 86 | +| SepalLength,SepalWidth,PetalLength,PetalWidth,Name | |
| 87 | +|----------------------------------------------------| |
| 88 | +| 5.1,3.5,1.4,0.2,Iris-setosa | |
| 89 | +| 7.0,3.2,4.7,1.4,Iris-versicolor | |
| 90 | +| 6.3,3.3,6.0,2.5,Iris-virginica | |
| 91 | + |
| 92 | + |
| 93 | +After the conversion, the resulting JSON data will look like below. |
| 94 | + |
| 95 | +```json |
| 96 | +[ |
| 97 | + { |
| 98 | + "SepalLength": "5.1", |
| 99 | + "SepalWidth": "3.5", |
| 100 | + "PetalLength": "1.4", |
| 101 | + "PetalWidth": "0.2", |
| 102 | + "Name": "Iris-setosa" |
| 103 | + }, |
| 104 | + { |
| 105 | + "SepalLength": "7.0", |
| 106 | + "SepalWidth": "3.2", |
| 107 | + "PetalLength": "4.7", |
| 108 | + "PetalWidth": "1.4", |
| 109 | + "Name": "Iris-versicolor" |
| 110 | + }, |
| 111 | + { |
| 112 | + "SepalLength": "6.3", |
| 113 | + "SepalWidth": "3.3", |
| 114 | + "PetalLength": "6.0", |
| 115 | + "PetalWidth": "2.5", |
| 116 | + "Name": "Iris-virginica" |
| 117 | + } |
| 118 | +] |
| 119 | +``` |
| 120 | + |
| 121 | +## Change log |
| 122 | + |
| 123 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 124 | + |
| 125 | +## Testing |
| 126 | + |
| 127 | +``` bash |
| 128 | +$ composer test |
| 129 | +``` |
| 130 | + |
| 131 | +## Known Issues |
| 132 | + |
| 133 | +Currently, it is assumed that each object shares the same properties while converting JSON to CSV. So if one object has a property that the other one does not have, then it will be a major problem. |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 138 | + |
| 139 | +## Credits |
| 140 | + |
| 141 | +- [Burak Özdemir][link-author] |
| 142 | +- [All Contributors][link-contributors] |
| 143 | + |
| 144 | +## License |
| 145 | + |
| 146 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 147 | + |
| 148 | +[ico-version]: https://img.shields.io/packagist/v/ozdemirburak/json-csv.svg?style=flat-square |
| 149 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 150 | +[ico-travis]: https://img.shields.io/travis/ozdemirburak/json-csv/master.svg?style=flat-square |
| 151 | +[ico-downloads]: https://img.shields.io/packagist/dt/ozdemirburak/json-csv.svg?style=flat-square |
| 152 | + |
| 153 | +[link-packagist]: https://packagist.org/packages/ozdemirburak/json-csv |
| 154 | +[link-travis]: https://travis-ci.org/ozdemirburak/json-csv |
| 155 | +[link-downloads]: https://packagist.org/packages/ozdemirburak/json-csv |
| 156 | +[link-author]: https://github.com/ozdemirburak |
| 157 | +[link-contributors]: ../../contributors |
0 commit comments