How to print JSON data to a Google Sheet using GSpread

Question:

I have tried every possible fix I can find online, unfortunately, I’m new to this and not sure if I’m getting closer or not.

Ultimately, all I am trying to do is print a JSON feed into a Google Sheet.

GSpread is working (I’ve appended just number values as a test), but I simply cannot get the JSON feed to print there.

I’ve gotten it printing to terminal, so I know it’s accessible, but writing the loop to append the data becomes the issue.

This is my current script:

# import urllib library
import json
from urllib.request import urlopen

import gspread


gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('1-1aiGMn2yUWRlh_jnIebcMNs-6phzUNxkktAFH7uY9o')
worksheet = sh.sheet1


# import json
# store the URL in url as
# parameter for urlopen


url = 'https://api.chucknorris.io/jokes/random'


# store the response of URL
response = urlopen(url)

# storing the JSON response
# from url in data
data_json = json.loads(response.read())

# print the json response
# print(data_json)
result = []
for key in data_json:
    result.append([key, data_json[key]])
worksheet.update('a1', result)

I’ve hit a complete brick wall – any advice would be greatly appreciated

Update – suggested script with new error:

# import urllib library
import json
from urllib.request import urlopen

import gspread


gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('1-1aiGMn2yUWRlh_jnIebcMNs-6phzUNxkktAFH7uY9o')
worksheet = sh.sheet1


url = 'https://api.chucknorris.io/jokes/random'


# store the response of URL
response = urlopen(url)

# storing the JSON response
# from url in data
data_json = json.loads(response.read())

# print the json response
# print(data_json)
result = []
for key in data_json:
    result.append([key, data_json[key] if not isinstance(
        data_json[key], list) else ",".join(map(str, data_json[key]))])
worksheet.update('a1', result)

Error:

Traceback (most recent call last):

  File "c:UsersAMadleNBA-JSON-FetchPrintToSheetTest.py", line 17, in <module>
    response = urlopen(url)
  File "C:Pythonpython3.10.5liburllibrequest.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:Pythonpython3.10.5liburllibrequest.py", line 525, in open
    response = meth(req, response)
  File "C:Pythonpython3.10.5liburllibrequest.py", line 634, in http_response
    response = self.parent.error(
  File "C:Pythonpython3.10.5liburllibrequest.py", line 563, in error
    return self._call_chain(*args)
  File "C:Pythonpython3.10.5liburllibrequest.py", line 496, in _call_chain
    result = func(*args)
  File "C:Pythonpython3.10.5liburllibrequest.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Can confirm it is not a permissions issue, the script below prints the same URL to terminal no problem. Also have no problem writing other data to the sheet:

import requests as rq
from bs4 import BeautifulSoup

url = 'https://api.chucknorris.io/jokes/random'

req = rq.get(url, verify=False)

soup = BeautifulSoup(req.text, 'html.parser')


print(soup)
Asked By: Anthony Madle

||

Answers:

In your script, I thought that it is required to convert the JSON data to a 2-dimensional array. And, when I saw the value of data_json, I noticed that an array is included in the value. I think that it is required to be also considered. I thought that this might be the reason for your issue. When this is reflected in your script, how about the following modification?

From:

result.append([key, data_json[key]])

To:

result.append([key, data_json[key] if not isinstance(data_json[key], list) else ",".join(map(str, data_json[key]))])
  • In this modification, the array is converted to the string using join.
Answered By: Tanaike