Converting JSON records into a CSV format is a frequent requirement for reporting and data exchange in modern PHP applications. Aspose.BarCode Cloud SDK for PHP provides a powerful API for generating barcode images and handling file transformations directly from your server. In this tutorial you will see a complete JSON to CSV code example in PHP that also embeds barcode images for each record. We will walk through the setup, the step‑by‑step implementation, a cURL alternative, and performance best practices.

How to Transform JSON Data to CSV in PHP - Step by Step

  1. Install the SDK via Composer: Use the official Composer package to add the library to your project.
composer require aspose-barcode-cloud
  1. Configure API credentials and create the BarCodeApi instance: Replace YOUR_APP_SID and YOUR_APP_KEY with your cloud credentials.
use Aspose\BarCode\Cloud\Configuration;
use Aspose\BarCode\Cloud\BarCodeApi;

$appSid = 'YOUR_APP_SID';
$appKey = 'YOUR_APP_KEY';
$config = new Configuration($appSid, $appKey);
$apiInstance = new BarCodeApi($config);

Reference: BarCodeApi class. 3. Open the source JSON file and the target CSV file: The script works with file streams to keep memory usage low.

$inputHandle  = fopen($inputJsonPath, 'rb');
$outputHandle = fopen($outputCsvPath, 'wb');
  1. Loop through JSON records, generate barcodes, and write CSV rows: Each record’s id field is turned into a barcode image, encoded in base64, and added to the CSV.
while (($line = fgets($inputHandle)) !== false) {
    $record = json_decode(trim($line), true);
    $barcodeValue = $record['id'] ?? '';
    $barcodeRequest = new GenerateBarcodeRequest([
        'type'   => $barcodeFormat,
        'text'   => (string)$barcodeValue,
        'format' => 'PNG',
        'resolutionX' => 300,
        'resolutionY' => 300
    ]);
    $barcodeResponse = $apiInstance->generateBarcode($barcodeRequest);
    $record['barcode_image'] = base64_encode($barcodeResponse);
    // Write header once, then rows
    // ...
}
  1. Clean up temporary files and close streams: The temporary folder is removed after processing.
fclose($inputHandle);
fclose($outputHandle);
array_map('unlink', glob($barcodeTempDir . '/*.png'));
rmdir($barcodeTempDir);

Generating CSV Output from JSON in PHP - Complete Code Example

The following example demonstrates the full implementation of reading a JSON file, creating barcode images, and writing the data to a CSV file using Aspose.BarCode Cloud SDK for PHP.

<?php
declare(strict_types=1);

// Autoload Composer dependencies (Aspose.BarCode Cloud SDK)
require __DIR__ . '/vendor/autoload.php';

use Aspose\BarCode\Cloud\Configuration;
use Aspose\BarCode\Cloud\BarCodeApi;
use Aspose\BarCode\Cloud\Model\GenerateBarcodeRequest;

// -------------------- Configuration --------------------
$appSid = 'YOUR_APP_SID';
$appKey = 'YOUR_APP_KEY';
$config = new Configuration($appSid, $appKey);
$apiInstance = new BarCodeApi($config);

// -------------------- Settings --------------------
$inputJsonPath   = __DIR__ . '/data/input.json';   // JSON source (one object per line)
$outputCsvPath   = __DIR__ . '/data/output.csv';   // Resulting CSV
$delimiter       = ';';                           // Custom CSV delimiter
$encoding        = 'UTF-8';                       // Target CSV encoding
$barcodeFormat   = 'Code128';                     // Barcode symbology
$barcodeField    = 'id';                          // JSON field to turn into barcode
$barcodeTempDir  = __DIR__ . '/temp_barcodes';    // Temporary folder for barcode images

// Ensure temporary directory exists
if (!is_dir($barcodeTempDir)) {
    mkdir($barcodeTempDir, 0777, true);
}

// -------------------- Open Files --------------------
$inputHandle  = fopen($inputJsonPath, 'rb');
if ($inputHandle === false) {
    throw new RuntimeException("Unable to open input JSON file.");
}
$outputHandle = fopen($outputCsvPath, 'wb');
if ($outputHandle === false) {
    fclose($inputHandle);
    throw new RuntimeException("Unable to open output CSV file.");
}

// Write UTF-8 BOM for Excel compatibility
fwrite($outputHandle, "\xEF\xBB\xBF");

// -------------------- Process JSON --------------------
$headerWritten = false;
while (($line = fgets($inputHandle)) !== false) {
    $line = trim($line);
    if ($line === '' || $line === '[' || $line === ']') {
        continue; // skip empty lines or array brackets
    }
    // Remove trailing commas if present (common in pretty‑printed arrays)
    $line = rtrim($line, ',');
    $record = json_decode($line, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        continue; // skip malformed lines
    }

    // Generate barcode image for the selected field
    $barcodeValue = $record[$barcodeField] ?? '';
    $barcodeRequest = new GenerateBarcodeRequest([
        'type' => $barcodeFormat,
        'text' => (string)$barcodeValue,
        'format' => 'PNG',
        'resolutionX' => 300,
        'resolutionY' => 300
    ]);
    try {
        $barcodeResponse = $apiInstance->generateBarcode($barcodeRequest);
        $barcodeFilePath = $barcodeTempDir . '/' . uniqid('barcode_', true) . '.png';
        file_put_contents($barcodeFilePath, $barcodeResponse);
        // Store relative path (or base64) in CSV; here we use base64 to keep CSV self‑contained
        $barcodeBase64 = base64_encode($barcodeResponse);
        $record['barcode_image'] = $barcodeBase64;
    } catch (Exception $e) {
        $record['barcode_image'] = '';
    }

    // Prepare CSV row with proper UTF‑8 encoding
    $csvRow = [];
    foreach ($record as $value) {
        if (is_array($value) || is_object($value)) {
            $value = json_encode($value, JSON_UNESCAPED_UNICODE);
        }
        $csvRow[] = mb_convert_encoding((string)$value, $encoding, 'UTF-8');
    }

    // Write header once
    if (!$headerWritten) {
        $header = array_keys($record);
        fputcsv($outputHandle, $header, $delimiter);
        $headerWritten = true;
    }

    // Write data row
    fputcsv($outputHandle, $csvRow, $delimiter);
}

// -------------------- Cleanup --------------------
fclose($inputHandle);
fclose($outputHandle);

// Optionally delete temporary barcode files
array_map('unlink', glob($barcodeTempDir . '/*.png'));
rmdir($barcodeTempDir);

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (input.json, output.csv, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Creating CSV via REST API from JSON Using cURL

You can achieve the same result without writing PHP code by calling the Aspose.BarCode Cloud REST endpoints directly. The workflow consists of uploading the JSON file, generating a barcode for each record, and downloading the final CSV.

  1. Obtain an access token (replace placeholders with your credentials).
curl -X POST "https://api.aspose.cloud/connect/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"

The response contains access_token.

  1. Upload the source JSON file.
curl -X PUT "https://api.aspose.cloud/v3.0/barcode/storage/file/input.json" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     --data-binary @data/input.json
  1. Generate a barcode for a single record (repeat for each record in a script).
curl -X POST "https://api.aspose.cloud/v3.0/barcode/generate" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "type":"Code128",
           "text":"12345",
           "format":"PNG",
           "resolutionX":300,
           "resolutionY":300
         }' \
     -o temp/barcode_12345.png
  1. Assemble the CSV file (you can use a simple script that reads the uploaded JSON, inserts the base64‑encoded barcode, and writes the CSV).
    The assembly step is performed locally; the API does not provide a direct JSON‑to‑CSV endpoint.

  2. Download the resulting CSV.

curl -X GET "https://api.aspose.cloud/v3.0/barcode/storage/file/output.csv" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o data/output.csv

For a complete list of parameters, see the official API documentation.

Getting the Environment Ready

To start coding, you need a PHP runtime (7.4 or later) and a valid Aspose.BarCode Cloud account.

composer require aspose-barcode-cloud

Download the SDK package from the official release page: Aspose.BarCode Cloud SDK for PHP Download. After installation, create a config.php file with your APP_SID and APP_KEY.

Optimizing Large JSON Processing Performance

  1. Stream the JSON file - read line by line instead of loading the whole file into memory.
  2. Reuse the BarCodeApi instance - creating a new client for each record adds overhead.
  3. Limit barcode resolution - 300 dpi is sufficient for most reports; lower values reduce payload size.
  4. Write CSV rows in batches - buffer a few rows before calling fputcsv to minimize disk I/O.

Practical Tips for Efficient JSON to CSV Conversion

  • Use a semicolon (;) or tab (\t) as the delimiter when your data may contain commas.
  • Encode non‑ASCII characters with UTF‑8 and prepend a BOM (\xEF\xBB\xBF) for Excel compatibility.
  • Store barcode images as Base64 strings inside the CSV to keep the file self‑contained.
  • Clean the temporary barcode folder after each run to avoid disk clutter.
  • Validate JSON lines with json_last_error() to skip malformed records gracefully.

Conclusion

This guide walked you through a complete JSON to CSV code example in PHP using the Aspose.BarCode Cloud SDK for PHP. You learned how to configure the SDK, generate barcodes for each record, handle custom delimiters and UTF‑8 encoding, and apply performance optimizations for large datasets. The SDK is a commercial product; you can purchase a license for production use or obtain a temporary license from the temporary license page to evaluate the library. Start integrating the code today and streamline your data‑exchange pipelines.

FAQs

Q: How does the JSON to CSV code example in PHP handle nested objects?
A: Nested arrays or objects are converted to a JSON string using json_encode before being written to the CSV, ensuring the CSV remains flat while preserving the original data. For more details, see the Aspose.BarCode Cloud SDK for PHP documentation.

Q: Can I change the CSV delimiter in the JSON to CSV script in PHP?
A: Yes, modify the $delimiter variable at the top of the script (e.g., set it to ',' for a comma‑separated file). The script respects any single‑character delimiter you provide.

Q: Is there a way to process thousands of JSON records efficiently?
A: Absolutely. The tutorial’s performance section recommends streaming the input file, reusing the API client, and writing rows in batches. These techniques reduce memory usage and speed up processing for large JSON files.

Q: Does the JSON to CSV utility in PHP support other barcode formats?
A: The $barcodeFormat variable can be set to any symbology supported by Aspose.BarCode, such as QR, DataMatrix, or Code39. Just update the value before running the script.

Read More