I have a json output from a regular Google Sheet Doc (following this guide) that outputs exactly this format
{
"range": "json!A1:Y1000",
"majorDimension": "ROWS",
"values": [
[
"nome",
"cognome",
"salary",
"assunzione",
"location",
"extension"
],
[
"james",
"di marcio",
"$85,675",
"09/12/2009",
"San Francisco",
"5384"
],
[
"marco",
"giaccanti",
"$54,560",
"06/06/2020",
"roma",
"5385"
]
]
}
and needs to be converted into this very format
{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Frank Major",
"position": "top manager",
"salary": "$320,800",
"start_date": "2011/06/20",
"office": "San Francisco",
"extn": "5221"
}
]
}
in order to be usable within this Datatables project.
The best I could come up with was this solution, which of course doesn't fit the required pattern AND only applies to the first of the arrays nested inside "values", while it would need to convert them all:
<?php
$json = file_get_contents('https://sheets.googleapis.com/v4/spreadsheets/[my_sheet_id]/values/json?key=[my_api_key]');
$objs = json_decode($json, false);
$codes = $objs->values[0];
$names = $objs->values[1];
foreach( $codes as $index => $code ) {
echo $code. ': ' . $names[$index]." ";
}
?>
nome: james cognome: di marcio salary: $85,675 assunzione: 09/12/2009 location: San Francisco extension: 5384
The idea was to set automatically the first array values as keys for the looping values, but of course the code is not enough ..
Any suggestion?
A side consideration: Datatables doesn't care which kind of file the table is fed. You may choose a .txt file as well as a .php file or I think may be even a .js file.
May be a little naif, I thought that .php was the best solution because -I thought- the work of processing is done server-side instead of client-side, resulting may be in a faster process. Is this the case?
ps: for duplicate seekers, I checked different sources before posting and none of them really fit my case