I am explaining you, how to generate JSON file with read content from Google spread sheet in php.
. if you have any requirement to read Google spead sheet and generate JSON file or want JSON type content so this post is very userful to you. I am explaining, you can get all content from Google spread sheet like JSON format. you need to generate google spread sheet from google Sheet section.
I have generated new spread Sheet from google and inserted some content.
after generated sheet you need to give full permission and made Publish to Web.
I have created new php file called readGoogleSheetGenerateJSON.php and also given generated file name with readGoogleSheetGenerateJSON.json. when you will hit in wamp server then it will generate json file.
Example :
<?php
$file="https://docs.google.com/spreadsheets/d/e/2PACX-1vQiIp4pdl9l1rvZ92VnvXPeXwDA5O8ByuJjNLq8X0F5GSeeuPqyDKiT5bfAWDvzqhctnbiqtJozHJZD/pub?output=csv";
function csvtojson($file,$delimiter)
{
if (($handle = fopen($file, "r")) === false)
{
die("can't open the file.");
}
$csv_headers = fgetcsv($handle, 4000, $delimiter);
$csv_json = array();
while ($row = fgetcsv($handle, 4000, $delimiter))
{
$csv_json[] = array_combine($csv_headers, $row);
}
fclose($handle);
return json_encode($csv_json, JSON_PRETTY_PRINT);
}
$jsonresult = csvtojson($file, ",");
echo $jsonresult;
if(file_put_contents('readGoogleSheetGenerateJSON.json', ($jsonresult)))
{
echo "success: JSON file readGoogleSheetGenerateJSON.json generated/updated";
}else
{
echo "failed";
}
?>
Output :
[ { "id": "1", "type": "website", "key": "website_id", "value": "1215451", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:25" }, { "id": "2", "type": "website", "key": "website_name", "value": "https:\/\/webdeveloperindia.in", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:25" }, { "id": "3", "type": "website", "key": "website_type", "value": "tutorial", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" }, { "id": "4", "type": "profile", "key": "user_id", "value": "123", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" }, { "id": "5", "type": "profile", "key": "user_email", "value": "[email protected]", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" }, { "id": "6", "type": "city", "key": "MP", "value": "indore", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" }, { "id": "7", "type": "city", "key": "MP", "value": "bhopal", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" }, { "id": "8", "type": "city", "key": "MP", "value": "khandwa", "created": "8\/16\/2022 6:10", "modified": "8\/16\/2022 16:26" } ]success: JSON file readGoogleSheetGenerateJSON.json generated/updated
when you will open json file in browser then it will show content like this format.
Hello, Welcome to webdeveloperindia.in. I am a full-stack web developer. Email – [email protected] | Skype – azaruddin23. I have knowledge of PHP, Laravel, Magento 1/2, Codeigniter, WordPress, Joomla, Shopify, Git, Bitbuket, jQuery, Ajax, Javascript and ReactJS.