question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid JSON payload received. Unexpected token. error when bulk uploading with update_cells(cell_list)

See original GitHub issue

Hello,

I started getting the error when I updated to Python 3.7 two days ago. I use the following code to upload a pandas df to a gsheet - the error happens with any df.

Re the error: this seems to pertain to some cells with value ‘180901_millbrae_1’, may or may not be relevant. This exact same data and code used to work fine.

Any ideas?

Error:

Traceback (most recent call last):

  File "<ipython-input-31-85b52784b441>", line 11, in <module>
    ws.update_cells(cell_list)

  File "/Users/../anaconda/lib/python3.7/site-packages/gspread/models.py", line 782, in update_cells
    'values': values_rect

  File "/Users/../anaconda/lib/python3.7/site-packages/gspread/models.py", line 176, in values_update
    r = self.client.request('put', url, params=params, json=body)

  File "/Users/../anaconda/lib/python3.7/site-packages/gspread/client.py", line 79, in request
    raise APIError(response)

APIError: {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\nae\", 43344, \"None\", NaN], [\"180901_millb\n                    ^",
    "status": "INVALID_ARGUMENT"
  }
}

Code:

n_rows, n_cols = df.shape
k = 1000
for i in range(n_rows//k + 1):    
    row0 = start_row + i*k
    col0 = start_col
    row1 = start_row + min((i+1)*k, n_rows-1)
    col1 = start_col + n_cols - 1
    cell_list = ws.range(inputRange(row0, col0, row1, col1))
    for cell in cell_list:
        val = df.iloc[cell.row-start_row, cell.col-start_col]
        cell.value = val
    ws.update_cells(cell_list)

Operating System: macOS Sierra 10.12.6 Python version: 3.7.0 gspread version: 3.1.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

44reactions
eanunezcommented, Dec 5, 2019

Hi, I encountered the same problem. I filled up ‘NaN’ values with empty string, df.fillna('', inplace=True) and the problem was solved.

5reactions
Imperssonatorcommented, Sep 23, 2020

I found an alternate solution that preserves the numeric data type of numeric columns:

worksht.update(
    [df.columns.values.tolist()] + [[vv if pd.notnull(vv) else '' for vv in ll] for ll in df.values.tolist()]
)

Probably quite slow if your sheet is large but it works 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error {{'code': 400, 'message': 'Invalid JSON payload ...
I am trying to run the following code for googleautoreply but it is giving the error 'Invalid JSON payload received.
Read more >
Invalid JSON payload received. Unexpected token. error when ...
Hello,. I started getting the error when I updated to Python 3.7 two days ago. I use the following code to upload a...
Read more >
Recieved error - Invalid JSON payload received, when trying ...
( 1 ), the error thrown is : Invalid JSON payload received. Unexpected token. Note: (1)Oauth credentials are checked and correct,. google access...
Read more >
Google Ads API and Invalid JSON payload received ... - Reddit
Google Ads API and Invalid JSON payload received. Unexpected token. I'm getting the following error when try to run my report that was ......
Read more >
Google Sheets - JSON Format | Blue Prism Product
Internal : Unexpected error Error during Web API HTTP Request HTTP Status Code: 400 ... "message": "Invalid JSON payload received. Unexpected token.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found