22
33[ ![ Latest Version on Packagist] [ ico-version ]] [ link-packagist ]
44[ ![ Software License] [ ico-license ]] ( LICENSE.md )
5- [ ![ Build Status] [ ico-travis ]] [ link-travis ]
5+ [ ![ Build Status] [ ico-github-actions ]] [ link-github-actions ]
66[ ![ Total Downloads] [ ico-downloads ]] [ link-downloads ]
77
88The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies.
@@ -11,96 +11,108 @@ The most basic CSV to JSON and JSON to CSV converter library in PHP without any
1111
1212Via Composer
1313
14- ``` bash
15- $ composer require ozdemirburak/json-csv
14+ ``` bash
15+ composer require ozdemirburak/json-csv
1616```
1717
18+ Note: Requires PHP 8.1 or higher with JSON extension
19+
1820## Usage
1921
2022### JSON to CSV Converter
2123
22- ``` php
24+ ``` php
2325use OzdemirBurak\JsonCsv\File\Json;
2426
2527// JSON to CSV
26- $json = new Json(__DIR__ . '/above .json');
28+ $json = new Json(__DIR__ . '/file .json');
2729// To convert JSON to CSV string
2830$csvString = $json->convert();
2931// To set a conversion option then convert JSON to CSV and save
3032$json->setConversionKey('utf8_encoding', true);
31- $json->convertAndSave(__DIR__ . '/above .csv');
33+ $json->convertAndSave(__DIR__ . '/file .csv');
3234// To convert JSON to CSV and force download on browser
3335$json->convertAndDownload();
3436```
3537
3638You can also convert directly from a JSON string using the ` fromString ` method.
3739
38- ``` php
39- $csvString = (new Json())->fromString('{"name": "Buddha ", "age": 80 }')->convert();
40+ ``` php
41+ $csvString = (new Json())->fromString('{"name": "John ", "age": 30 }')->convert();
4042```
4143
42- Assume that the input JSON is something like below.
44+ Assume that the input JSON is something like below.
4345
4446``` json
4547[
4648 {
4749 "name" : {
48- "common" : " Turkey " ,
49- "official" : " Republic of Turkey " ,
50- "native" : " T \u00fc rkiye "
50+ "common" : " Türkiye " ,
51+ "official" : " Republic of Türkiye " ,
52+ "native" : " Türkiye "
5153 },
5254 "area" : 783562 ,
5355 "latlng" : [39 , 35 ]
5456 },
5557 {
5658 "name" : {
57- "common" : " Israel " ,
58- "official" : " State of Israel " ,
59- "native" : " \u05d9\u05e9\u05e8\u05d0\u05dc "
59+ "common" : " Japan " ,
60+ "official" : " Japan " ,
61+ "native" : " 日本 "
6062 },
61- "area" : 20770 ,
62- "latlng" : [31.30 , 34.45 ]
63+ "area" : 377975 ,
64+ "latlng" : [36 , 138 ]
6365 }
6466]
6567```
6668
6769After the conversion, the resulting CSV data will look like below.
6870
69- ** name\_ common** |** name\_ official** |** name\_ native** |** area** |** latlng\_ 0** |** latlng\_ 1**
70- :-----:|:-----:|:-----:|:-----:|:-----:|:-----:
71- Turkey|Republic of Turkey|Türkiye|783562|39|35
72- Israel|State of Israel|ישראל|20770|31.3|34.45
71+ | name_common | name_official | name_native | area | latlng_0 | latlng_1 |
72+ | --------------| ----------------------| --------------| ------------| ----------| -----------|
73+ | Türkiye | Republic of Türkiye | Türkiye | 783562 | 39 | 35 |
74+ | Japan | Japan | 日本 | 377975 | 36 | 138 |
75+
76+ #### JSON Conversion Options
7377
78+ | Option | Type | Default | Description |
79+ | -----------------| --------| -----------| --------------------------------------|
80+ | ` delimiter ` | string | ` , ` | CSV field delimiter |
81+ | ` enclosure ` | string | ` " ` | CSV field enclosure character |
82+ | ` escape ` | string | ` \ ` | CSV escape character |
83+ | ` join ` | string | ` _ ` | Character used to join nested keys |
84+ | ` null ` | mixed | ` null ` | Value to use for null/missing fields |
85+ | ` utf8_encoding ` | bool | ` false ` | Add UTF-8 BOM to output |
7486
7587### CSV to JSON Converter
7688
77- ``` php
89+ ``` php
7890use OzdemirBurak\JsonCsv\File\Csv;
7991
8092// CSV to JSON
81- $csv = new Csv(__DIR__ . '/below .csv');
93+ $csv = new Csv(__DIR__ . '/file .csv');
8294$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
8395// To convert CSV to JSON string
8496$jsonString = $csv->convert();
8597// To convert CSV to JSON and save
86- $csv->convertAndSave(__DIR__ . '/below .json');
98+ $csv->convertAndSave(__DIR__ . '/file .json');
8799// To convert CSV to JSON and force download on browser
88100$csv->convertAndDownload();
89101```
90102
91103You can also convert directly from a CSV string using the ` fromString ` method.
92104
93- ``` php
94- $jsonString = (new Csv())->fromString('[{ "name":"Buddha"," age":"80"}]' )->convert();
105+ ``` php
106+ $jsonString = (new Csv())->fromString("name, age\nJohn,30\n" )->convert();
95107```
96108
97- Assume that the input CSV file is something like below.
109+ Assume that the input CSV file is something like below.
98110
99- ** SepalLength** | ** SepalWidth** | ** PetalLength** | ** PetalWidth** | ** Name**
100- : -----:|: -----:|: -----:|: -----:|: -----:
101- 5.1| 3.5| 1.4|0.2|Iris-setosa
102- 7.0| 3.2| 4.7|1.4|Iris-versicolor
103- 6.3| 3.3| 6.0| 2.5| Iris-virginica
111+ | SepalLength | SepalWidth | PetalLength | PetalWidth | Name |
112+ | ------------- | ------------- | -------------- | ------------ | ----------------- |
113+ | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
114+ | 7.0 | 3.2 | 4.7 | 1.4 | Iris-versicolor |
115+ | 6.3 | 3.3 | 6.0 | 2.5 | Iris-virginica |
104116
105117After the conversion, the resulting JSON data will look like below.
106118
@@ -130,20 +142,27 @@ After the conversion, the resulting JSON data will look like below.
130142]
131143```
132144
145+ #### CSV Conversion Options
146+
147+ | Option | Type | Default | Description |
148+ | -------------| --------| -----------| --------------------------------------------------------|
149+ | ` delimiter ` | string | ` , ` | CSV field delimiter |
150+ | ` enclosure ` | string | ` " ` | CSV field enclosure character |
151+ | ` escape ` | string | ` \ ` | CSV escape character |
152+ | ` join ` | string | ` _ ` | Character used to split nested keys |
153+ | ` numeric ` | bool | ` false ` | Set to ` true ` to convert numeric strings to numbers |
154+ | ` options ` | int | ` 0 ` | JSON encode flags (e.g., ` JSON_PRETTY_PRINT ` ) |
155+
133156## Change log
134157
135158Please see [ CHANGELOG] ( CHANGELOG.md ) for more information what has changed recently.
136159
137160## Testing
138161
139- ``` bash
140- $ composer test
162+ ``` bash
163+ composer test
141164```
142165
143- ## Known Issues
144-
145- Currently, there are not any issues that are known.
146-
147166## Contributing
148167
149168Please see [ CONTRIBUTING] ( CONTRIBUTING.md ) for details.
@@ -159,11 +178,11 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
159178
160179[ ico-version ] : https://img.shields.io/packagist/v/ozdemirburak/json-csv.svg?style=flat-square
161180[ ico-license ] : https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
162- [ ico-travis ] : https://img.shields.io/travis/ ozdemirburak/json-csv/master.svg? style=flat-square
181+ [ ico-github-actions ] : https://img.shields.io/github/actions/workflow/status/ ozdemirburak/json-csv/tests.yml?branch=master& style=flat-square
163182[ ico-downloads ] : https://img.shields.io/packagist/dt/ozdemirburak/json-csv.svg?style=flat-square
164183
165184[ link-packagist ] : https://packagist.org/packages/ozdemirburak/json-csv
166- [ link-travis ] : https://travis-ci.org /ozdemirburak/json-csv
185+ [ link-github-actions ] : https://github.com /ozdemirburak/json-csv/actions/workflows/tests.yml
167186[ link-downloads ] : https://packagist.org/packages/ozdemirburak/json-csv
168187[ link-author ] : https://github.com/ozdemirburak
169188[ link-contributors ] : ../../contributors
0 commit comments