phpgoogle-sheets-api

PHP / Google Sheets API : Uncaught Google\Service\Exception Invalid JSON payload received. Unknown name \"0\" at 'data.values[0]


I'm using PHP to read/write data in a Google Sheet but I'm getting the error Uncaught Google\Service\Exception Invalid JSON payload received. Unknown name "0" at 'data.values[0].

I have seen many posts with the same error but I can't figure out what's wrong with my payload. The script that I'm using is parsing more than 1000 lines successfully but suddenly it breaks on one particular line and I don't see why.

Here is the PHP part:

$updateRow = [$articles[$key]["id"],$articles[$key]["language"],$articles[$key]["rawurl"],$articles[$key]["publishdate"],$articles[$key]["modifieddate"],$articles[$key]["internal_title"],$articles[$key]["internal_desc"],$articles[$key]["seo_title"],$articles[$key]["seo_desc"],$articles[$key]["product"],$articles[$key]["score"],$articles[$key]["tags"],$articles[$key]["categories"],$articles[$key]["ads"]];
$rows = [$updateRow];
$valueRange = new \Google_Service_Sheets_ValueRange();
$valueRange->setValues($rows);
$range = 'Blog_Metadata!A'.$position; 
$options = ['valueInputOption' => 'USER_ENTERED'];
output($valueRange);
output($options);
$service->spreadsheets_values->update($spreadsheetId, $range, $valueRange, $options);

This works fine for all the other entries. Let's now get into the details of the payload.

$article[$key] contains the following:

Array
(
[id] => 2187
[rawurl] => nl%2Froadtrip%2Frondreis-italie%2Fcomo-meer
[permalink] => nl/roadtrip/rondreis-italie/como-meer
[publishdate] => 1519675325
[modifieddate] => 1519675325
[language] => nl
[translations] => Array
    (
        [fr] => 2128
        [nl] => 2187
        [en] => 2177
        [es] => 
        [de] => 
        [it] => 
    )

[translations_url] => Array
    (
        [fr] => fr%2Fcircuits%2Fcircuit-italie%2Flac-de-come%2F
        [nl] => nl%2Froadtrip%2Frondreis-italie%2Fcomo-meer%2F
        [en] => en%2Froadtrips%2Froadtrip-italy%2Flake-como-visit%2F
        [es] => 
        [de] => 
        [it] => 
    )

[ads] => https://www.booking.com/searchresults.nl.html?ss=como&aid=324758
[internal_title] => Comomeer
[internal_desc] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
[seo_title] => Comomeer - Reisverhaal (activiteiten, foto' s,...)
[seo_desc] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
[product] => 
[score] => 
[tags] => 
[categories] => rondreis-italie
[thumbnail] => https://images.laurentwillen.be/sites/21/2018/02/lac-de-come-top-300x94.jpg
[thumbnail_large] => https://images.laurentwillen.be/sites/21/2018/02/lac-de-come-top-1400x438.jpg
[thumbnail_max] => https://images.laurentwillen.be/sites/21/2018/02/lac-de-come-top-1900x594.jpg
)

Empty fields are very common, it's not specific to this one.

The content of $updateRow is:

Array
(
[0] => 2187
[1] => nl
[2] => nl%2Froadtrip%2Frondreis-italie%2Fcomo-meer
[3] => 1519675325
[4] => 1519675325
[5] => Comomeer
[6] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
[7] => Comomeer - Reisverhaal (activiteiten, foto' s,...)
[8] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
[9] => 
[10] => 
[11] => 
[12] => rondreis-italie
[13] => https://www.booking.com/searchresults.nl.html?ss=como&aid=324758
)

the content is $valueRange is:

Google_Service_Sheets_ValueRange Object
(
[collection_key:protected] => values
[majorDimension] => 
[range] => 
[values] => Array
    (
        [0] => Array
            (
                [0] => 2187
                [1] => nl
                [2] => nl%2Froadtrip%2Frondreis-italie%2Fcomo-meer
                [3] => 1519675325
                [4] => 1519675325
                [5] => Comomeer
                [6] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
                [7] => Comomeer - Reisverhaal (activiteiten, foto' s,...)
                [8] => Een echt concentraat van Italië met prachtige landschappen, bioscoopstukken, jetset, geschiedenis en goede (ijs)ballen.
                [9] => 
                [10] => 
                [11] => 
                [12] => rondreis-italie
                [13] => https://www.booking.com/searchresults.nl.html?ss=como&aid=324758
            )

    )

[internal_gapi_mappings:protected] => Array
    (
    )

[modelData:protected] => Array
    (
    )

[processed:protected] => Array
    (
    )

)

The JSON payload looks fine to me. I have compared it with payloads that worked but I see no difference. I sometimes have empty entries and it works. For some reason, quotes are not correctly rendered here but I have entries with quotes before this one and everything works well.

Do you see anything that would explain why my scripts breaks at that specific point?

thanks


Solution

  • From your showing sample values, I guessed that the null value might be the reason for your current issue of Invalid JSON payload received. Unknown name \"0\" at 'data.values[0]. If my understanding is correct, how about the following modification?

    From:

    $rows = [$updateRow];
    

    To:

    $rows = [array_map(function($e) {
      return (is_null($e)) ? "" : $e;
    },$updateRow)];
    

    or, I thought that this might be able to be also used.

    $rows = [array_map('strval', $updateRow)];