Thursday, October 9, 2025
HomeLanguagesHow to Convert JSON file into CSV in PHP ?

How to Convert JSON file into CSV in PHP ?

In this article, we are going to see how to convert JSON data into a CSV file using PHP.

JSON (JavaScript Object Notation) is a dictionary-like notation that can be used to structuring data. It is stored with the extension .json, for example – neveropen.json

On the other hand, CSV (or Comma Separated Value) files represent data in a tabular format, with several rows and columns. An example of a CSV file can be an Excel Spreadsheet. These files have the extension of .csv, for example – neveropen.csv.

Requirements: XAMPP Server

Structure of JSON:

[{
    "data1": "value1", 
    "data2": "value2", 
    ..., 
    "data n": "value n"
}]

Example:

[{
    "student": "sravan kumar",
    "age": 22,
    "subject": "java"
}]

Used Methods:

  1. json_decode() Method: This function is used to decode or convert a JSON object to a PHP object.

    Syntax:

    json_decode( string, assoc )
    
    
    

    Example:

    $jsondata = '[{
        "student": "sravan kumar",
        "age": 22,
        "subject": "java"
    },
    {
        "student": "ojaswi",
        "age": 21,
        "subject": "java"
    },
    { 
        "student": "rohith",
        "age": 22,
        "subject": "dbms"
    },
    {
        "student": "bobby",
        "age": 22,
        "subject": "sql"
    }]';
    
    // Decode the json data and convert it
    // into an associative array
    $jsonans = json_decode($jsondata, true);
    
  2. fopen() Method: It is used to open a file.

    Syntax:

    fopen( filename, file_mode )

    Example:

    // File pointer in writable mode
    $file_pointer = fopen($csv, 'w');
    
  3. fclose() Method: It is used to close the file.

    Syntax:

    fclose( $file_pointer );

    Example:

    fclose( $file_pointer );
  4. fputcsv() Method: It is used to place the data into CSV file.

    Syntax:

    fputcsv( file, fields )

    Example:

    fputcsv( $file_pointer, $i );
    1. Steps to Run:

  • Start XAMPP server

  • Open notepad and type the following code in json.php and place it under htdocs folder.

PHP code:

PHP




<?php
   
// Student JSON data
$jsondata
  '[
   {"student":"sravan kumar","age":22,"subject":"java"},
   {"student":"ojaswi","age":21,"subject":"java"},
   {"student":"rohith","age":22,"subject":"dbms"},
   {"student":"bobby","age":22,"subject":"sql"}]';
   
// Decode json data and convert it
// into an associative array
$jsonans = json_decode($jsondata, true);
   
// CSV file name => neveropen.csv
$csv = 'neveropen.csv';
   
// File pointer in writable mode
$file_pointer = fopen($csv, 'w');
   
// Traverse through the associative
// array using for each loop
foreach($jsonans as $i){
      
    // Write the data to the CSV file
    fputcsv($file_pointer, $i);
}
   
// Close the file pointer.
fclose($file_pointer);
  
?>


Output: Type localhost/json.php in your browser. You can see the CSV file is created with the file name as neveropen.csv

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS