json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 57358 (char 57357) - python

I am getting a JSON response from the server and storing it in a text file
Later I parse certain data from the text file, but while doing so I am getting the above error. The JSON format seems to be okay to me Can you please help me here. Thanks Also I do not have any control over the response I receive from the server. For now, I am just using dump to write it to a text file
Code for json dump and load
balance = exchange.fetch_balance()
#print(balance)
with open('balance.txt', 'w') as json_file:
json.dump(balance, json_file)
print("Balance file created")
with open('balance.txt') as json_file:
data = json.load(json_file)
freeusdt = float(data['info']['balances'][11]['free'])
Qty=float(df['close'][last_row_index])
Qty=Qty+300
buyqty=freeusdt/Qty
My JSON file is here (in the .txt file Since StackOverflow won't let me post the full code as it exceeds the character limit)
https://drive.google.com/file/d/1kpBMl6JbzlH9OQvvteLl0Ieb2PaBU_kr/view?usp=sharing
The full error is
Traceback (most recent call last):
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 232, in <module>
schedule.run_pending()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 780, in run_pending
default_scheduler.run_pending()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 100, in run_pending
self._run_job(job)
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 172, in _run_job
ret = job.run()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 661, in run
ret = self.job_func()
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 223, in run_bot
check_buy_sell_signals(supertrend_data)
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 121, in check_buy_sell_signals
data1 = json.load(json_file1)
File "C:\Users\pavan.alur\Miniconda3\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\pavan.alur\Miniconda3\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\pavan.alur\Miniconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\pavan.alur\Miniconda3\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 57358 (char 57357)

The indentation problem as mentioned by #MattDMo solved it
balance = exchange.fetch_balance()
with open('balance.txt', 'w') as json_file:
json.dump(balance, json_file)
print("Balance file created")
with open('balance.txt') as json_file:
data = json.load(json_file)
freeusdt = float(data['info']['balances'][11]['free'])
Qty=float(df['close'][last_row_index])
Qty=Qty+300
buyqty=freeusdt/Qty

Related

Python Request Error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) [duplicate]

I'm retrieving data from one of my endpoints:
for index in self.indices_to_fetch:
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
print(response)
back-end:
#users_blueprint.route('/fetch_one_image', methods=['POST'])
def fetch_one_image():
post_data = request.get_json()
index = post_data.get('index')
image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()
response_obj = {
'image_name': image_name
}
return jsonify(response_obj), 200
Error output:
...
...
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/kivy/uix/screenmanager.py", line 419, in _on_complete
self.screen_in.dispatch('on_enter')
File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "main.py", line 338, in on_enter
self.display_results()
File "main.py", line 383, in display_results
self.fetch_one_image()
File "main.py", line 342, in fetch_one_image
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
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'm using similar approach on my other endpoints but getting no errors. Only this one is bugging me. What can I do ?
Change
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
to
response = requests.post('http://localhost/fetch_one_image', json={'index': index})
and take a look at what is actually returned.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
usually means something else, not JSON, was returned.
Maybe the problem is
image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()
as that actually fetches a Photo object (or perhaps even None, if the filter [in combination with .fist()] returns nothing), not only it's name. Try something like [it's just a guess, since I don't know your model structure]
image = Photo.query.filter(Photo.owner.has(User.id==id)).first()
response_obj = {'image_name': image.name}
return jsonify(response_obj), 200

Unable to read API responce data

I am trying to get data from the below seen URLs
import requests
import json
service_a = requests.get('https:xxxxx')
service_b = requests.get('https:xxxxx')
service_a.json()
service_b.json()
While for the case of service_a, service_a.json() gives me the expected data, for service_b, the call service_b.json() gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 79, in scan_once
return _scan_once(string, idx)
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 70, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Why is this happening?
ps. I notced that when I try to visit the https link of service_a, the data it contains are shown in the browser, while for sevice_b, no data are shown in the browser, but when I click it a file with its data is downloaded in my computer.
Try to check the Content-Type of service_b response.
import csv
response = requests.get(API_URL)
decoded_data = response.content.decode("utf-8")
csv_data = csv.reader(decoded_data.splitlines(), delimiters=",")
csv_list = list(csv_data)
in most cases your
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
error is due to:
non-JSON conforming quoting
XML/HTML output (that is, a string starting with <), or
incompatible character encoding

Requests throws json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I'm retrieving data from one of my endpoints:
for index in self.indices_to_fetch:
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
print(response)
back-end:
#users_blueprint.route('/fetch_one_image', methods=['POST'])
def fetch_one_image():
post_data = request.get_json()
index = post_data.get('index')
image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()
response_obj = {
'image_name': image_name
}
return jsonify(response_obj), 200
Error output:
...
...
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/kivy/uix/screenmanager.py", line 419, in _on_complete
self.screen_in.dispatch('on_enter')
File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "main.py", line 338, in on_enter
self.display_results()
File "main.py", line 383, in display_results
self.fetch_one_image()
File "main.py", line 342, in fetch_one_image
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
File "/home/mark/front_end_android/venv/lib/python3.7/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
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'm using similar approach on my other endpoints but getting no errors. Only this one is bugging me. What can I do ?
Change
response = requests.post('http://localhost/fetch_one_image', json={'index': index}).json()
to
response = requests.post('http://localhost/fetch_one_image', json={'index': index})
and take a look at what is actually returned.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
usually means something else, not JSON, was returned.
Maybe the problem is
image_name = Photo.query.filter(Photo.owner.has(User.id==id)).first()
as that actually fetches a Photo object (or perhaps even None, if the filter [in combination with .fist()] returns nothing), not only it's name. Try something like [it's just a guess, since I don't know your model structure]
image = Photo.query.filter(Photo.owner.has(User.id==id)).first()
response_obj = {'image_name': image.name}
return jsonify(response_obj), 200

Issues reading json from txt file

I have a json string in a txt file and I'm trying to read it to do some other procedures afterwards. It looks like this:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.load(f)
I know the json is valid, but I'm getting:
Traceback (most recent call last):
File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
user_input()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
child_remover()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
x = json.load(f)
File "C:\Python33\lib\json\__init__.py", line 274, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Python33\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python33\lib\json\decoder.py", line 370, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I used this website to check if the string is valid. If I use .loads(), I get a different error:
Traceback (most recent call last):
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
user_input()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
child_remover()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
x = json.loads(f)
File "C:\Python33\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
Originally the json was embeded in my script like this:
json_text="""json stuff here"""
And didn't get any errors. Any ideas on how to fix this???
Running python 3.3.3 just in case.
Thanks!!
EDIT:
Just some random (valid) json on the txt and I get the same issue. This os one of the ones i tried:
{"data":
{"mobileHelp":
{"value":
{
"ID1":{"children": [1,2,3,4,5]},
"ID2":{"children": []},
"ID3":{"children": [6,7,8,9,10]}
}
}
}
}
Which is valid as well as per jsonlint.com.
Your file contains a UTF-8 BOM character at the start. UTF-8 doesn't need a BOM but especially Microsoft tools insist on adding one anyway.
Open the file with the utf-8-sig encoding instead:
>>> open('/tmp/json.test', 'wb').write(b'\xef\xbb\xbf{"data":\r\n {"mobileHelp":\r\n {"value":\r\n {\r\n "ID1":{"children": [1,2,3,4,5]},\r\n "ID2":{"children": []},\r\n "ID3":{"children": [6,7,8,9,10]}\r\n }\r\n }\r\n }\r\n}')
230
>>> import json
>>> with open('/tmp/json.test', encoding='utf8') as f:
... data = json.load(f)
...
Traceback (most recent call last):
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 271, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 369, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> with open('/tmp/json.test', encoding='utf-8-sig') as f:
... data = json.load(f)
...
>>> data
{'data': {'mobileHelp': {'value': {'ID2': {'children': []}, 'ID3': {'children': [6, 7, 8, 9, 10]}, 'ID1': {'children': [1, 2, 3, 4, 5]}}}}}
Note that from Python 3.4 onwards you get a more helpful error message here:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 314, in loads
raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
ValueError: Unexpected UTF-8 BOM (decode using utf-8-sig)
Not sure what your code looks like for the second error, but it looks like you are passing json.loads a file object and not a string. Try:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.loads(f.read())
or without newlines with:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.loads(f.read().replace('\n', ''))
As another choice, This will be much easier to fix this issue.
json.loads(open('test.txt').read().decode('utf-8-sig'))

Geocoding a list of addresses from a CSV file

I am trying to geocode a list of addresses in a CSV file. The column with the information is named "full" and it looks like this:
full
100 Ross St,15219
1014 Blackadore Ave.,15208
1026 Winterton St.,15206
...
This is the code that I am using:
import csv
import pygeocoder
import pandas as pd
from pygeocoder import Geocoder
df = pd.read_csv('C:\Users\Jesus\Desktop\Events.csv')
address = df.full
result = Geocoder.geocode(address)
print(result[0].coordinates)
And this is the output:
Traceback (most recent call last):
File "C:\Users\Jesus\Desktop\python\geocode.py", line 10, in <module>
result = Geocoder.geocode(address)
File "C:\Python27\lib\site-packages\pygeocoder.py", line 160, in geocode
return GeocoderResult(Geocoder.get_data(params=params))
File "C:\Python27\lib\site-packages\pygeocoder.py", line 107, in get_data
response_json = response.json()
File "C:\Python27\lib\site-packages\requests\models.py", line 693, in json
return json.loads(self.text, **kwargs)
File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 488, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 389, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 122, in scan_once
return _scan_once(string, idx)
File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 118, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
JSONDecodeError: Expecting value: line 1 column 1 (char 0
Your address variable is a Series object from pandas, which may be causing this problem. To geocode all the addresses from the CSV, iterate through it like:
for a in address:
result = Geocoder.geocode(a)
print(result[0].coordinates)
To store the results in a file (assuming Python 2.x):
with open('filename', 'w') as outfile:
for a in address:
result = Geocoder.geocode(a)
print >>outfile, str(result[0].coordinates) # Prints to file
You can do outfile.write( str(result[0].coordinates) ) instead of the print if you want. The only difference is that print automatically adds a line break. To add to a list, just declare the list outside of your for statement (such as coordinates_list = []) and replace the print with coordinates_list.append(result[0].coordinates). Either of these ways will work in Python 3.x, but the print >>outfile statement will not.

Categories

Resources