How do I stop this decode error in python Json - python

im practicing with python json, and i made a function that opens a file, and loads the number that was stored in that file, but if that file does not exist, it excepts the error, and ask for input for a favorite number, then dumps that number in the file, then loads it. but i keep getting an error
import json
def get_num():
filename = 'numberssfresdfs.json'
try:
with open(filename) as num:
number = json.load(num)
except FileNotFoundError:
favorite_num = input("Please enter your favorite number ")
with open(filename, 'w') as num:
json.dump(favorite_num, num)
print("Your favorite number is " )
else:
print("I know your favorite number, its " + str(number))
get_num()
I keep getting this error
Traceback (most recent call last):
File "remember_favorite_num.py", line 16, in <module>
get_num()
File "remember_favorite_num.py", line 7, in get_num
number = json.load(num)
File "C:\Users\kenda\AppData\Local\Programs\Python\Python36-
32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\kenda\AppData\Local\Programs\Python\Python36-
32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\kenda\AppData\Local\Programs\Python\Python36-
32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\kenda\AppData\Local\Programs\Python\Python36-
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)

You are never reaching the code in your except block because numberssfresdfs.json does exist.
You are getting the json.load error message because numberssfresdfs.json does not contain valid json or is empty.
You can use a json linter (one online linter) to validate that your file contains valid json.
Once you confirm you are working with valid json, you can work through the rest of your logic.

Related

JSONDecodeErrror while trying to decide JSON from file

I am trying to create a very simple blockchain with Python.
import json
import os
import hashlib
blockchain_dir = os.curdir + "/blockchain/"
The code can save blocks, but if want to use the check_integrity function I get an JSONDecodeError:
def get_hash(filename):
file = open(blockchain_dir + filename, "rb").read()
return hashlib.md5(file).hexdigest()
def get_files():
files = os.listdir(blockchain_dir)
return sorted([int(i) for i in files])
def check_integrity():
files = get_files()
results = []
for file in files[:1]:
f = open(blockchain_dir + str(file))
h = json.load(f)['hash']
prev_file = str(file - 1)
actual_hash = get_hash(prev_file)
if h == actual_hash:
res = 'OK'
else:
res = 'Corrupted'
results.append({'block': prev_file, 'result': res})
return results
def write_block(name, amount, to_whom, prev_hash=''):
files = get_files()
prev_file = files[-1]
file_name = str(prev_file + 1)
prev_hash = get_hash(str(prev_file))
data = {"name": name,
"amount": amount,
"to_whom": to_whom,
"hash": prev_hash}
with open(blockchain_dir + file_name, "w") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
def main():
write_block("Friend", 120, "Me")
print(check_integrity())
if __name__ == "__main__":
main()
Traceback (most recent call last):
File "/Python/blockchain/block.py", line 67, in <module>
main()
File "/Python/blockchain/block.py", line 64, in main
print(check_integrity())
File "/Python/blockchain/block.py", line 31, in check_integrity
h = json.load(f)['hash'] # method load - take object and return json object, hash - it is key in the block file, we get hash from the block file and compare it with hash that we get after function get_hash
File "/usr/lib/python3.9/json/__init__.py", line 293, in load
return loads(fp.read(),
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)
What is the problem, and how can I fix it?
The issue is that the json file you're trying to parse is empty.I'm not familiar with any method to automatically check if a json file is empty, apart from checking for such an occurrence manually. You can manually check and confirm whether a json file is empty by adding a try-except statement in your check integrity function i.e:
try:
h = json.load(f)['hash']
except KeyError:
...
The keyerror catches a scenario where the "hash" key is empty therefore suggesting the data your trying to access doesn't exist.

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) while opening json file

i am trying to create blog page using flask and i want to take input file as a config.json which i have created. Please help me i am getting json decoder error
i also tried to convert str to utf8 but its showing the error
with open('config.json', 'r', encoding ='utf-8') as c:
params = json.load(c)["params"]
json content:
{
"params":
{
"local_server":"True",
"local_uri":"mysql://root:#localhost/codingthunder",
"prod_uri":"mysql://root:#localhost/codingthunder",
"fb_url":"https://facebook.com/codingthunder",
"tw_url":"https://twitter.com/codingthunder",
"gh_url":"https://github.com/codingthunder",
"blog_name":"Coding Thunder",
"tag_line":"A Blog liked by Programmers"
}
}
output log:
PS C:\Users\ASHISH\Desktop\Coding\Flask> python -u "c:\Users\ASHISH\Desktop\Coding\Flask\Blog Page\main.py"
Traceback (most recent call last):
File "c:\Users\ASHISH\Desktop\Coding\Flask\Blog Page\main.py", line 7, in <module>
params = json.load(c)["params"]
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\ASHISH\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\ASHISH\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)
Expecting value: line 1 column 1 (char 0) hints that there is no value at the beginning of the file, so no content in the parsed file.
This could mean, that
the file is empty
you opened the wrong or a non existing file
you are using a relative path and your working direktory is wrong (e.g. the direktory you execute your program in)

JSON decode error since I switched from macOS to Windows

I have a Python file that worked perfectly on my macbook.
Once I moved it to window, for some reason it gave me new error.
this is the errors im getting:
Traceback (most recent call last):
File "C:/Users/chadi/PycharmProjects/untitled4/main.py", line 3, in <module>
from main_controller import Ui_MainController
File "C:\Users\chadi\PycharmProjects\untitled4\main_controller.py", line 6, in <module>
from get_companies import load_companies
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 10, in <module>
json_read('convertcsv.json')
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 8, in json_read
data = (json.load(f_in))
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\chadi\anaconda3\envs\untitled4\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)
Process finished with exit code 1
this is all the parts where i load a JSON file:
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
json_read('convertcsv.json')
def mark_employee_unavailability(service,employee,reason,start_date,end_date):
with open('info_inspecteurs.json') as json_file:
data = json.load(json_file)
for i in range(len(data)):
if data[i]['n_inspecteur'] == employee:
event_email = data[i]['email_inspecteur']
break
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
def get_employee_info(n_inspecteur):
json_read('info_inspecteurs.json')
value = list(filter(lambda x: x["n_inspecteur"] == n_inspecteur, data))[0]
if len(value) > 0:
print(value['ad_inspecteur'], "\n", value['email_inspecteur'])
return (value['ad_inspecteur'],value['email_inspecteur'])
print("no record found")
return None
def load_employees_from_info_inspecteurs():
json_read('info_inspecteurs.json')
employees=[]
for company in data:
employees.append(company['n_inspecteur'])
return employees
I don't know where it came from, or maybe is the new environement I used on Windows? I used anaconda. Does it change anything? I was on VENV on my macOS.
Thank you for your help
The error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
may just mean that there's no data being read from the target file at all, so json isn't finding any data to parse.
You might troubleshoot by trying to just read the file from that point in your script into a string - if it fails, the problem may be in python execution current directory / relative file path, rather than in the json parsing.
with open ("convertcsv.json", "r") as checkFile:
checkData = checkFile.read()
# print out length or contents of checkData or sim.

Error in reading JSON: No JSON object could be decoded

I am reading a set of JSON files using glob and storing them in a list. The length of the list is 1046. When I am reading the JSON file one by one and loading it to run further code, it just runs on 595 files and gives the following error:
Traceback (most recent call last):
File "removeDeleted.py", line 38, in <module>
d = json.load(open(fn))
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I am loading the json files like this:
json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json"))
for fn in json_file_names:
#print fn
#temp = temp + 1
#count = 0
d = json.load(open(fn))
objects = d["objects"]
for j in range(len(objects)):
Can anybody suggest me way out of this error?
As Blender said, you need to find out which of your files contains invalid JSON. To this end, you need to add some debugging statements to your code:
json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json"))
for fn in json_file_names:
#print fn
#temp = temp + 1
#count = 0
try:
d = json.load(open(fn))
objects = d["objects"]
for j in range(len(objects)):
except ValueError as e:
print "Could not load {}, invalid JSON".format({})
One of your json text files is empty. Maybe start by seeing if you have any zero size files with
find . -size 0
run from your directory of json files in a terminal.

Invalid Control Character in JSON decode (using Python)

def list(type, extra=""):
if extra != "":
entity = "http://api.crunchbase.com/v/1/" + type + "/" + extra + ".js?api_key=" + key
data = json.load(urllib2.urlopen(entity))
else:
entity = "http://api.crunchbase.com/v/1/" + type + ".js?api_key=" + key
data = json.load(urllib2.urlopen(entity))
return data
The function list is called specifically here:
x = colink
details = list(co, x)
specifically on the instance where x is "if_this_then_that" and co is "company"
The code breaks down on this line when I query on the second line (the entity link is properly formatted). The Error Message is below and the line in the JSON file where the error occurs follows. I am not sure how to handle the unicode error when getting data through a JSON API. Any suggestions on how to remedy this would be appreciated.
Traceback (most recent call last):
File "crunch_API.py", line 95, in <module>
details = list(co, x)
File "crunch_API.py", line 34, in list
data = json.load(urllib2.urlopen(entity))
File "C:\Python27\lib\json\__init__.py", line 278, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 24 column 89 (char 881)
"overview": "\u003Cp\u003EIFTTT is a service that lets you create powerful connections with one simple statement: if this then that.\u003C/p\u003E", #### Where the error occurs

Categories

Resources