CSV to JSON Conversion in PHP

Have you been utilizing CSV for data storage? If so, you’re not alone; its ability to store large amounts of information in a compact file, makes the CSV format a great choice for organization. However, if you need a widescale format for editing purposes, converting a CSV file to JSON could be your best option. The following API will enable you to convert your data sets from CSV to JSON using PHP.

First, we will run this command to install the SDK:

After the installation, we are ready to call the conversion function:

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\ConvertDataApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$column_names_from_first_row = true; // bool | Optional; If true, the first row will be used as the labels for the columns; if false, columns will be named Column0, Column1, etc. Default is true. Set to false if you are not using column headings, or have an irregular column structure.
try {
$result = $apiInstance->convertDataCsvToJson($input_file, $column_names_from_first_row);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataCsvToJson: ', $e->getMessage(), PHP_EOL;
}
?>

The returned result will provide you with your newly created JSON file. To retrieve your personal API key, head to the Cloudmersive website to register for a free account and receive access to 800 monthly calls across our multitude of APIs.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store