How to convert JSON to CSV in PHP

Here I am going to show you how to convert JSON into CSV using PHP programming language. I will show you both on how to convert JSON string or JSON file to CSV string or file. In my previous example I had shown you how to convert CSV to JSON in PHP.

JSON is an acronym and stands for JavaScript Object Notation which is very light-weight and at present has becoming a standard response format for REST APIs. CSV stands for Comma Separated Values which is again plain text and easily readable and parse-able by most of the programming languages.

Prerequisites

PHP 7.4.3, Apache 2.4 HTTP Server

Project Directory

It’s assumed that you have setup Apache and PHP.

Now we will create a project root directory called php-json-csv under the Apache server’s htdocs folder.

I may not mention the project root directory in subsequent sections and I will assume that we are talking with respect to the project root directory.

JSON string to CSV string

Here I am going to show you how you can read JSON string and convert it to a corresponding CSV string. The following code is written into a PHP file called php-json-csv.php. I have set the output header with plain/text as CSV values are represented in plain text.

<?php

header('Content-type: text/plain; charset=UTF-8');

$json = '{
    "1": {
        "student_id": "1",
        "student_dob": "01-01-1980",
        "student_email": "sumit@email.com",
        "student_address": "Garifa"
    },
    "2": {
        "student_id": "2",
        "student_dob": "01-01-1982",
        "student_email": "gourab@email.com",
        "student_address": "Garia"
    },
    "3": {
        "student_id": "3",
        "student_dob": "01-01-1982",
        "student_email": "debina@email.com",
        "student_address": "Salt Lake"
    },
    "4": {
        "student_id": "4",
        "student_dob": "01-01-1992",
        "student_email": "souvik@email.com",
        "student_address": "Alipore"
    },
    "5": {
        "student_id": "5",
        "student_dob": "01-01-1990",
        "student_email": "liton@email.com",
        "student_address": "Salt Lake"
    }
}';

$array = json_decode($json, true);

$csv = '';

$header = false;
foreach ($array as $line) {	
    if (empty($header)) {
        $header = array_keys($line);
		$csv .= implode(',', $header);
        $header = array_flip($header);		
    }
	
	$line_array = array();
	
	foreach($line as $value) {
		array_push($line_array, $value);
	}
	
	$csv .= "\n" . implode(',', $line_array);
}

//output as CSV string
echo $csv;

JSON file to CSV file

The following code example shows how to read JSON file and write to CSV file using PHP program. The input JSON file is kept under same directory as PHP file is kept. The output CSV file is generated under the same directory where input file is kept.

<?php

header('Content-type: text/plain; charset=UTF-8');

$file = "student.json";
$json = file_get_contents($file);

$fcsv = fopen('student.csv', 'w');
$array = json_decode($json, true);

$csv = '';

$header = false;
foreach ($array as $line) {	
    if (empty($header)) {
        $header = array_keys($line);
        fputcsv($fcsv, $header);
        $header = array_flip($header);		
    }
	
	$line_array = array();
	
	foreach($line as $value) {
		array_push($line_array, $value);
	}

    fputcsv($fcsv, $line_array);
}

//close CSV file after write
fclose($fcsv);

Any combination from the following you can try:

  • JSON string to CSV String
  • JSON file to CSV string
  • JSON string to CSV file
  • JSON file to CSV file

Testing the Application

Running the PHP file in browser, you will see the following output:

convert json to csv in php

The same output you will find in the generated output CSV file.

Source Code

Download

Leave a Comment