How to Easily Convert Data to JSON in PHP
Data stored in JSON format is generally much easier to work with. However, we don’t always get to choose what format our data arrives in.
Thankfully, we can convert several common data formats to JSON by calling a few free APIs with complementary PHP code examples. We can quickly and easily convert CSV, XLSX (Excel), and XML to JSON format with zero hassle.
We can call all three of these APIs using the code provided below, and we can run a single command to install the client SDK for all three. In addition, we can use a single Cloudmersive API key to authorize our data conversion requests (this will allow us to make up to 800 API calls with zero commitments).
To install the PHP client with Composer, let’s execute the following command from our command line:
composer require cloudmersive/cloudmersive_document_convert_api_client
With that out of the way, we can copy the code for the conversion we need.
We can use the following code to convert CSV data to JSON (note that we can set the $column_names_from_first_row
parameter to customize how our columns are labeled):
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// 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;
}
?>
And we can use the below code to convert XLSX (Excel) to JSON:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// 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.
try {
$result = $apiInstance->convertDataXlsxToJson($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataXlsxToJson: ', $e->getMessage(), PHP_EOL;
}
?>
Last on our list, we can use the remaining code to convert XML to JSON:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// 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.
try {
$result = $apiInstance->convertDataXmlToJson($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDataApi->convertDataXmlToJson: ', $e->getMessage(), PHP_EOL;
}
?>
That’s all the code we’ll need! Now we can easily convert a few common data formats to JSON in our PHP application.