Reputation: 318
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
Upvotes: 0
Views: 464
Reputation: 855
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
Upvotes: 1