JSONDecodeError: Expecting value Python - python

The error "expecting value" is occurring when I'm trying to read a json file which I have created myself. The weird thing is that this used to work. I was working on another project in between and did some updates to conda and now I'm getting the error. I've searched and found similar questions but none that have been applicable in my case.
This is what I'm doing right now:
def show_json(filepath):
with open("/Users/human/Doc/PYTHON/bn/bread/"+filepath) as f:
k = json.load(f)
return k
state_d = show_json("states_json.json")
Here is the error traceback:
File "/Users/human/PYTHON/SPYDER/bn/bread/main_.py", line 55, in <module>
state_d=show_json("states_json.json")
File "/Users/human/PYTHON/SPYDER/bn/bread/main_.py", line 48, in show_json
k = json.load(f)
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
And this is what the json file looks like:
{"_state_one_":None,"_state_two_":None,"_state_three_":None,"_state_four_":None}
Could something have happened while updating some libraries and packages or am I just doing something wrong code-wise?

Your JSON file is simply not compliant to the JSON specifications. Shortly put, null should be used instead of None

Related

VK_API: Problems with music, json decoder

I'm trying to make a special parser for VK, which downloads all music from page of some user, but there's some problem with vk_api, which allows to access audiofiles. I'm trying to call method get() to get list of all tracks, but launch of this program:
session = vk_api.VkApi(token=tkn)
vk = session.get_api()
vk_audio = audio.VkAudio(session)
def get_list_audio():
dct = vk_audio.get(owner_id=owner_id, album_id=None, access_hash=None)
return dct
print(get_list_audio())
shows nothing but an error, connected with json decoder:
Traceback (most recent call last):
File "C:\Users\ann\PycharmProjects\vk-parser\main.py", line 44, in <module>
print(get_list_audio())
File "C:\Users\ann\PycharmProjects\vk-parser\main.py", line 38, in get_list_audio
dct = vk_audio.get(owner_id=owner_id, album_id=None, access_hash=None)
File "D:\PycharmProjects\vk-parser\lib\site-packages\vk_api\audio.py", line 158, in get
return list(self.get_iter(owner_id, album_id, access_hash))
File "D:\PycharmProjects\vk-parser\lib\site-packages\vk_api\audio.py", line 107, in get_iter
response = self._vk.http.post(
File "D:\PycharmProjects\vk-parser\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Python 3.9\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Python 3.9\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python 3.9\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Please, give some hints, what can I do?
vk_api/audio.py makes just POST request to m.vk.com/audio:
and the error occures when it tries to convert the response to json (line 119). If we run these lines manually (or you can use debugger anytime to see what happens), we see 302 http error with empty response. json() can't get json from this empty response, so you see the exception. You have to choose another library (to work with vk api) or to write these actions yourself. Or to fix this lib code :)
Probably, this vk_api library does not support the changes that have occurred in vk.com. You can take this code from the library and make your own method for getting audio based on it.

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) in Python application

I have written an python program which basically interacts with Google Cloud Storage Buckets. I wanted to read the the contents of multiple JSON files(At present I can read one JSON file pretty easily).However I am facing an issue. Here's my code.
credential_path = "xyz.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
os.environ['GCS_API_KEY'] = "164641r3t4fcvfsv564"
storage_client = storage.Client()
bucket_name = 'NAME'
file = '2021-03-05'
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(prefix=file)
for blob in blobs:
print(blob.name)
data = json.loads(blob.download_as_string(client=None))
print(data)
So what This piece of code does is that it reads the entire bucket with the file '2021-03-05' and simply prints the names of json files and their contents.
The issue I am facing is found here:
File "app.py", line 19, in <module>
data = json.loads(blob.download_as_string(client=None))
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I have tried working with a json.load() or with read() however that didn't work. Please help me out. How can I loop through all the files and get their names and read their content? Please help.

JSONDecodeError When using YFinance for downloading data

I am getting the following error when I am trying to download data using yfinance library. The strange fact is that this is the code I run all days to download data. I was thinking of a server error but I really don't know.
This morning it gives me this warning that doesn't stop the code to run but it actually stops data from downloading.
Do you have any guess?
Thank you very much
import pandas as pd
import yfinance as yf
import datetime as dt
tickers = ["BTC-USD", "XLM-USD", "XRP-USD", "XMR-USD", "LTC-USD"]
df = pd.DataFrame()
ohlcv = {}
start = dt.datetime.today()-dt.timedelta(3000)
end = dt.datetime.today()-dt.timedelta(0)
for ticker in tickers:
df[ticker] = yf.download(ticker,start,end, interval = "1wk")['Adj Close'].dropna()
Exception in thread Thread-30:
Traceback (most recent call last):
File "C:\Users\LorenzoFancelli\Anaconda3\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\LorenzoFancelli\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\site-packages\yfinance\multi.py", line 167, in _download_one_threaded
actions, period, interval, prepost, proxy, rounding)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\site-packages\yfinance\multi.py", line 182, in _download_one
rounding=rounding, many=True)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\site-packages\yfinance\base.py", line 155, in history
data = data.json()
File "C:\Users\LorenzoFancelli\Anaconda3\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\LorenzoFancelli\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\LorenzoFancelli\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This happened to me as I updated my virtualenv from Python 3.7 to 3.8. Uninstalling yfinance and reinstalling it solved the issue.
Check this post too: https://github.com/ranaroussi/yfinance/issues/318

python-twitter stream raises JSONDecodeError

When calling for the next tweet of the stream with next(), the following error occurs. When I try calling other functions with the same twitter API object, such as GetFriends(), it works fine. Any help is much appreciated!
Here is the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/twitter/api.py", line 4897, in _ParseAndCheckTwitter
data = json.loads(json_data)
File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
And here is the code:
import twitter
class Topic:
def __init__(self, topic):
self.api = self.get_twitter_api()
self.stream = self.api.GetStreamFilter(track=[topic])
self.count_tweets()
def get_twitter_api(self):
with open('/twitter_credentials.json') as f:
return twitter.Api(**eval(f.read()))
def count_tweets(self):
while next(self.stream):
print("tweet")
The oldest trick in the book worked: restarting the PC.

Can't get a valid response from a webpage containing json data

I've written a script in python to get response from a webpage. The data in that webpage are in json format. However, when I try like below I get an error. Can somebody give me any workaround as to how I can get a valid response?
Here is my failure attempt:
import requests
import json
URL = "https://www.sandicormls.com/agent/AgentSearch?officeSortOption=name&_=1516998894917&_keywordsAll=&officeLocation=&sortOption=rndsrt&_keywordsAgentName=&page=&typeIn=Realtor%2CBroker%2COwner%2COffice+Manager%2CAppraiser&searchMode=agent&officeName="
res = requests.get(URL,headers={'User-Agent':'Mozilla/5.0'})
print(res.json())
This is the traceback:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python35-32\new_line_one.py", line 34, in <module>
print(res.json())
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I just gave it a try and confirm that your code is fine as it works on my environment. Though, I run it on a Linux (Ubuntu 16.04) virtual machine.
You could check what data you are getting back
print(res.headers[‘content-type’])
or examine the content such as
print(res.text)

Categories