Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleSheets API:"Invalid JSON payload received. Unknown name "Number" at 'data.values[0]': Cannot find field

I am trying to copy contents of a spreadsheet into another spreadsheet but the error keeps popping up. Could you help to figure out why is it not copying over the data from one sheet to the other? This is the dummy data I am experimenting on in the original sheet:

1   CHANGED London 
2   5   Budapest
3   4   Prague
4   11  Madrid
4   11  Madrid

This is how the destination sheet looks like:

2   5   Budapest
2   5   Budapest
2   5   Budapest

This is part of the error I get:

Invalid JSON payload received. Unknown name "Weather" at 'data.values[0]': Cannot find field.
Invalid JSON payload received. Unknown name "City" at 'data.values[0]': Cannot find field.
Invalid JSON payload received. Unknown name "Number" at 'data.values[1]': Cannot find field.
Invalid JSON payload received. Unknown name "Weather" at 'data.values[1]': Cannot find field.
Invalid JSON payload received. Unknown name "City" at 'data.values[1]': Cannot find field.
Invalid JSON payload received. Unknown name "Number" at 'data.values[2]': Cannot find field.

And this is the script:

from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope )
client = gspread.authorize(creds)

LT = client.open("LetsTry").sheet1
LT_data = LT.get_all_records()
NQ_all = client.open("NQ_all").sheet1
NQ_all_data = NQ_all.get_all_records()

service = discovery.build('sheets', 'v4', credentials=creds)
spreadsheetId = 'xyz'
range = 'Sheet1!A1:C11'
body = {
    'values': LT_data
}
result = service.spreadsheets().values().append(
    spreadsheetId=spreadsheetId, 
    range = range,
    valueInputOption= 'RAW',
    body=body).execute()
print('{0} cells updated.'.format(result.get('updatedCells')))
like image 467
Zsuzsanna Szucs Avatar asked Oct 29 '25 22:10

Zsuzsanna Szucs


1 Answers

I had the same problem and I recognized that it's because of encoding. I just set all data to utf8 and the error is fixed.

utf8_encode($yourValue);

Here is the utf encoding : https://www.php.net/manual/tr/function.utf8-encode.php

like image 152
Enver Avatar answered Oct 31 '25 13:10

Enver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!