while url:
post = session.post(login, data=payload)
r = session.get(url)
parsed = json.loads(r.text)
# Retrieve json product data
if parsed['links']['next'] is not 'null':
url = 'https://testshop.example.com/admin/products' + str(parsed['links']['next'])
time.sleep(2)
for product in parsed['products']:
parsed_result = product['id']
else:
print('stop now!')
break
SO I am using the code above to retrieve and print all the json data in my terminal. Everything is going fine until I retrieve the following error code at the end:
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
Does anybody know what the cause is of this and how I can fix it?
This is my JSON format if that matters:
products: [
{
article_code: "123",
barcode: "456",
brand_id: 2600822,
created_at: "2018-05-31T15:15:34+02:00",
data01: "",
data02: "",
data03: "",
delivery_date_id: null,
has_custom_fields: false,
has_discounts: false,
has_matrix: false,
hits: 0,
hs_code: null,
id: 72660113,
image_id: null,
is_visible: false,
price_excl: 33.0165,
price_incl: 39.95,
price_old_excl: 0,
price_old_incl: 0,
product_set_id: null,
product_type_id: null,
search_context: "123 456 789",
shop_id: 252449,
sku: "789",
supplier_id: 555236,
updated_at: "2018-05-31T15:15:34+02:00",
variants_count: 1,
visibility: "hidden",
weight: 0,
nl: {
content: "",
fulltitle: "Grid Lifter",
slug: "grid-lifter",
title: "Grid Lifter"
}
],
links: {
first: ".json",
last: ".json?page=70",
prev: null,
next: ".json?page=2",
count: 3497,
limit: 50,
pages: 70
}
I am using this to paginate through all the pages.
Traceback:
File "", line 1, in
runfile('loginlightspeedshop.py', wdir='C:/Users/Solaiman/.spyder-py3/SCRIPTS/Lightspeed scripts')
File "sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Solaiman/.spyder-py3/SCRIPTS/Lightspeed scripts/loginshop.py", line 33, in
parsed = json.loads(r.text)
File "C:\Users\Solaiman\Anaconda3\lib\json__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
You are probably getting empty/not json response here:
r = session.get(url)
Try to print r.text before parsing it to detect problem cause. Or use try/except clause:
try:
parsed = r.json()
except ValueError:
print(r.text)
break
Related
I am trying make Microservice warehouse / store. Everything is ok, but when I want to connect celery to my project, I am getting error
Expecting value: line 1 column 1 (char 0) when trying to start celery
#shared_task
def shop_sync():
url = 'http://warehouse:8001/authors/'
response_author = requests.get(url=url).json()
while 1:
for counter, data in enumerate(response_author['results']):
Author.objects.get_or_create(
id=data['id'],
defaults={
'id': data['id'],
'first_name': data['first_name'],
'last_name': data['last_name']
}
)
if response_author['next']:
response_author = requests.get(response_author['next']).json()
else:
break
url = 'http://warehouse:8001/genres/'
response_genre = requests.get(url).json()
while 1:
for counter, data in enumerate(response_genre['results']):
Genre.objects.get_or_create(
id=data['id'],
defaults={
'id': data['id'],
'name': data['name']
}
)
if response_genre['next']:
response_genre = requests.get(
response_genre['next']
).json()
else:
break
url = 'http://warehouse:8001/books/'
response = requests.get(url).json()
while 1:
for counter, data in enumerate(response['results']):
book, created = Book.objects.get_or_create(
id=data['id'],
defaults={
'id': data['id'],
"title": data['title'],
"description": data['description'],
"image": data['image'],
"language": data['language'],
"status": data['status'],
"price": data['price'],
"isbn": data['isbn'],
"pages": data['pages'],
"created": data['created'],
"available": data['available'],
"quantity": data['quantity'],
"genre": Genre.objects.get(id=data['genre'])
}
)
if not created:
book.title = data['title']
book.description = data['description']
book.image = data['image']
book.language = data['language']
book.status = data['status']
book.price = data['price']
book.isbn = data['isbn']
book.pages = data['pages']
book.created = data['created']
book.available = data['available']
book.quantity = data['quantity']
book.genre = Genre.objects.get(id=data['genre'])
book.save()
for i in data['author']:
author = Author.objects.get(id=i)
book.author.add(author)
if response['next']:
response = requests.get(response['next']).json()
else:
break
print('Sync is done')
Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 450, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 731, in __protected_call__
return self.run(*args, **kwargs)
File "/code/shop/tasks.py", line 105, in shop_sync
response = requests.get(url=url).json()
File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/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)
To avoid such issues in your code you should first check the response status code and then based on the status do appropriate action. Such as :-
url = 'http://warehouse:8001/books/'
response = requests.get(url)
if response.status_code != 200:
return
response_data = response.json()
while 1:
for counter, data in enumerate(response_data['results']):
book, created = Book.objects.get_or_create(
id=data['id'],
...
...
)
I am trying to read the content of a json file with the function defined below but I keep getting the following traceback:
def clean_email_list_no_slash(json_obj):
dirty_dict = {}
with open(json_obj, 'r') as rdable:
r = json.load(rdable)
dirty_dict.update(r)
for v in dirty_dict.values():
empty_list = []
for email in v['email']:
if not ('/' in email) or not ('\\' in email):
empty_list.append(email)
v['email'] = empty_list
with open(json_obj, 'w') as wtable:
json.dump(dirty_dict, wtable)
print('Done.')
Traceback (most recent call last):
File "c:/Users/EM/Desktop/Scripts/nocws/draft.py", line 306, in <module>
clean_email_list_no_slash(json_obj)
File "c:/Users/EM/Desktop/Scripts/nocws/draft.py", line 292, in clean_email_list_no_slash
r = json.loads(rdable)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\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 don't get where I am making a mistake.
Here is a sample of the json file:
{
"afghanistan": {
"website": "http://olympic.af/",
"facebook links": [
"https://facebook.com/OlympicOfficial.af"
],
"email": [
"info#olympic.af"
],
"email_links": [
"http://olympic.af/contact-us/"
],
"list_of_urls": [
"http://olympic.af/",
"http://olympic.af/contact-us/"
]
},
"albania": {
...
}
}
I'm trying to parse a website with the requests module on AWS EC2.
import requests
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
print(result.text)
The page is responding as below:
{
"arrangetype":"U",
"list": [
{
"product_no":43,
"display_order":4,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
},
{
"product_no":44,
"display_order":6,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
}
],
"length":2
}
so I did as below thanks to Roadrunner's help(parsing and getting list from response of get request
)
import requests
from json import loads
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
json_dict = loads(result.text)
print([x["product_no"] for x in json_dict["list"]])
It is perfectly working on local PC(windows10), but now i'm working on AWS EC2 to run the code, however, it occurs error.
The name of the file is timesale.py btw.
Traceback (most recent call last):
File "timesale.py", line 657, in <module>
Timesale_Automation()
File "timesale.py", line 397, in Timesale_Automation
status_check()
File "timesale.py", line 345, in status_check
json_dict = loads(now_sale_page2.text)
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)
I couldn't figure out why this error occurs.
I looked up overstackflow and there were some articles about decoding json but
result.text.decode('utf-8')
returns, I cannot decode str....
interestingly, I put print(result) between the codes and it is not 'None'
Why this would happen?
I'm having an issue I can't understand. I sending a json data as string via Redis (as a queue) and the receiver is throwing the following error :
[ERROR JSON (in queue)] - {"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature":
"6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"} => end is out of bounds
The code that throws the exception is the following :
try:
item = json.loads(item[1])
except ValueError as e:
sys.stderr.write("[ERROR JSON (in queue)] - {1} => {0}\n".format(str(e), str(item)))
return None
What is really odd, is that if I open a python console and do the following :
>>> import json
>>> s = '{"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature": "6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"}'
>>> print s
I have no issue, the string (copy/pasted in the Python console) yield no errors at all, but my original code is throwing one!
Do you have any idea about what is causing the issue?
You are loading item[1], which is the second character of the string items:
>>> json.loads('"')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: end is out of bounds
You should write:
item = json.loads(item)
So, I've been trying to load a json file using json.load. It is the simplest, most basic code in the world but this is the error that is being thrown followed by the code and then a snippet of the .json file (in case there is a formatting issue):
Error:
Traceback (most recent call last):
File "first.py", line 8, in <module>
data = json.load(input_file)
File /usr/lib/python3.4/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Code:
import json
from pprint import pprint
input_file = open("chennai.recent.02dec2015.jsons",'r', encoding = 'UTF-8')
data = json.load(input_file)
pprint (data)
.json snippet:
{"contributors": null, "truncated": false, "text": "#Chennai Floods Leave Thousands Stranded, More Rain Expected For Coming Days #jobs #500K #: India has deployed... "is_quote_status": false, "in_reply_to_status_id": null, "id": 672203575703965696, "favorite_count": 0