I have scraped some html and want to create a json doc. Here is the code I currently have:
with open(path.join(path.abspath(path.curdir),'Results\\html.txt'), 'r') as file:
for line in file.readlines():
if not line.strip():
continue
if re.findall(r'\"aggregateRating.*\"telephone\"',line):
reviews = re.findall(r'\[.*\]', line)
json_data = json.loads(str(reviews))
The error I get is: json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
Any help is appreciate. I have been stuck on this for awhile..
Your code is trying to load the string representation of a list as a valid json string; which of course will not work.
It is the same as trying to do this:
>>> json.loads(str(['hello world']))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/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 2 (char 1)
If you are trying to write the result as json; you need to do the opposite of loads, which is dumps:
>>> json.dumps(str(['hello world']))
'"[\'hello world\']"'
Related
Following code was used in Thonny (offline) IDE and it works fine. But when I use the same code in trinket it gives me an error. What could be the reason?
def compare_prices(product_laughs,product_glomark):
html_lau=requests.get(product_laughs).content
html_glo=requests.get(product_glomark).content #get the content of the site
soup_lau=BeautifulSoup(html_lau,'html.parser')
soup_glo=BeautifulSoup(html_glo,'html.parser')
glo_products=soup_glo.find("script", type="application/ld+json")
glomark_content=glo_products.text #glomark product list
print(glomark_content)
productList=json.loads(glomark_content)
This last statement gives the following error
Traceback (most recent call last):
File "/tmp/sessions/96aa9a060805dc3c/main.py", line 4, in <module>
compare_prices(laughs_coconut,glomark_coconut)
File "/tmp/sessions/96aa9a060805dc3c/compare_prices.py", line 27, in compare_prices
productList=json.loads(glomark_content)
File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.9/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 want to parse a json file and print the data in console.
I have a json file as input.json in which the content is :
{ "name":"John", "age":30, "city":"New York"}
Now I'm running a python script in the same directory where the input file is stored but getting the error. The python script looks like this :
import json
# Opening JSON file
f = open('input.json','r')
data = json.load(f)
print(data)
Error is:
File "C:\Users\madhav\Desktop\New folder (2)\instaclient\req.py", line 9, in <module>
data = json.load(f)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\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)
I am creating a website using Django .
Below is my code:
views.py:
for i in range(0,len(userdata)):
json_hall = requests.get(
"https://www.onebookingsystem.com/API/Admin/booking_list_id.php?id=%s" % userdata[i]['bookid'])
r = json_hall.json()
hall_data = json.loads(json_hall.text)
id_data[i]['bookid'] = hall_data[0]['bookid']
When i run i am getting the error like this.
File "D:\KarthikWorkSpace\Projects\Python\obs_admin\Application\obs_app\views.py", line 1968, in bookings_owner
hall_data = json.loads(json_hall.text)
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Any help would be appreciated.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This usually means that the string you're trying to decode is empty, eg:
>>> import json
>>> json.loads('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/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)
Please, make sure the request you are sending is correct.
You could print or inspect response.text to confirm that.
When using requests, the .json() method on a response object already gives you a Python object (decoded from the literal response)
You don't need to parse again the data with json.loads. By the way, here's a more Pythonic way to iterate over your data:
for (idx, user) in enumerate(userdata):
response = requests.get(
"https://www.onebookingsystem.com/API/Admin/booking_list_id.php?id=%s" % user['bookid'])
hall_data = response.json()
id_data[idx]['bookid'] = hall_data[0]['bookid']
for i in os.listdir("./saves/"):
path = './saves/' + i
if path.endswith(".json"):
with open(path, "r"):
config = json.loads(path)
ctime = config["Creation Time"]
Okay, this is the small code that's causing errors. I'm getting a json decoder error Here is the callback
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/acrobat/PycharmProjects/Jan9coderun/TranslatorVCS/Quiz.py", line 318, in saveGame
config = json.loads(path)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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)
My saves folder is in the same working directory as my code and has one quiz.json, one notes.txt, and one __init__.py file.
I make a requests on IMDb to fetch movie information into JSON. Here's my code:
import json
import requests
url = "http://www.imdb.com/title/tt3385516/"
re = requests.get(url).json()
It get me an error which I don't know what to do with it:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/requests/models.py", line 799, in json
return json.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
I tried using
re = requests.get(url)
data = json.loads(re.text)
But it get me another error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
Response is HTML, not JSON so it can't be JSON decoded:
>>> r = requests.get('http://www.imdb.com/title/tt3385516/')
>>> r.headers['content-type']
'text/html;charset=UTF-8'