Open In App
Related Articles

How to generate Json File in PHP ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation.

Structure:

{"data":[
 { "sub_data1":"value1", "sub_data2":"value2","sub_data_n":"value n" },
 { "sub_data2":"value2","sub_data2":"value2", "sub_data_n":"value n" },
 { "sub_data n":"value n ", "sub_data2":"value2","sub_data_n":"value n" }
]}

Example:

[{"id":"7020","name":"Bobby","Subject":"Java"},
 {"id":"7021","name":"ojaswi","Subject":"sql"}]

Properties:

  1. JSON doesn’t use an end tag
  2. It is shorter.
  3. It is quicker to read and write.
  4. It can use arrays.

Approach: In this article, we can generate JSON data using an array., create an array

Syntax:

$array = Array (
   "number" => Array (
       "data1" => "value1",
       "data2" => "value2",
       "data n" => "valuen"
   ),
   "number" => Array (
      "data1" => "value1",
      "data2" => "value2",
      "data n" => "valuen"
   )
);

Example:

$array = Array (
   "0" => Array (
       "id" => "7020",
       "name" => "Bobby",
       "Subject" => "Java"
   ),
   "1" => Array (
        "id" => "7021",
       "name" => "ojaswi",
       "Subject" => "sql"
   )
);

Use json_encode() to convert the array to JSON. It is used to convert an array to JSON

Syntax:

json_encode(array_input);

Example: Place the file in the path using file_put_contents()

$json = json_encode($array);

The file_name is the JSON to be saved and json_object is the object after JSON from the array is created.

Syntax:

file_put_contents(file_name.json.json_object);

Example:

file_put_contents("geeks_data.json", $json);

PHP code 1: The following code generates a JSON file using PHP. We have taken the $myJson variable which stores the name, age, city, and profession of a candidate, and generates a JSON file using the json_encode() and file_put_contents() methods.

PHP

<?php
$myJson = new stdClass();
$myJson->name = "Rani";
$myJson->age = 40;
$myJson->city = "Hyderabad";
$myJson->profession = "Doctor";
 
$myJSONvar = json_encode($myJson);
 
echo $myJSONvar;
 
// Generate json file
file_put_contents("result_data.json", $myJSONvar);
?>

                    

Output: The JSON “result_data.json” file is also created in the root folder with the following data.

{"name":"Rani","age":40,"city":"Hyderabad","profession":"Doctor"}

PHP code 2: This is another way of showing the file generation from the given array.

PHP

<?php
   
// Input data  through array
$array = Array (
    "0" => Array (
        "id" => "7020",
        "name" => "Bobby",
        "Subject" => "Java"
    ),
    "1" => Array (
         "id" => "7021",
        "name" => "ojaswi",
        "Subject" => "sql"
    )
);
 
// Encode array to json
$json = json_encode($array);
 
// Display it
echo "$json";
 
// Generate json file
file_put_contents("geeks_data.json", $json);
?>

                    

Output:

[{"id":"7020","name":"Bobby","Subject":"Java"},
 {"id":"7021","name":"ojaswi","Subject":"sql"}]
  • The JSON file is created in your path.
  • The data present in the created file

geeks_data



Last Updated : 01 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads