For a couple of days i've been trying to learn how to use JSON in python, but even if i copy everything from tutorials it still won't load.
import json
data = '''
{
"people":[
{
"name": "veljko",
"age":"20",
"email": veljkov02#gail.com,
"educated": "true"
} ,
{
"name":"aleksandar",
"age":"24",
"email":"marko99#gmail.com",
"educated":"false"
}
]
}
'''
men = json.loads(data)
print(men)
(Ignore the random words used in code, it's just how i learn)
It just keeps giving the same error messages, i've tried with the most simple JSON but it's still the same.
Traceback (most recent call last):
File "C:\Users\*********\Desktop\code3\json.py", line 21, in <module>
men = json.loads(data)
File "C:\Users\************\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\*************\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode.
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\************\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 7 column 14 (char 75)
Process finished with exit code 1
Please help, i'm a begginer and am not sure how to solve this problem.
You are missing quotes around veljkov02#gail.com. You need to change it to "veljkov02#gail.com", so that it is interpreted as a string. The exception tells you which line and character the error is at, so double check that line when you get errors like these.
Related
Im getting the error whenever i try to run this code in visual studio code.
Image of code
Error message:
Traceback (most recent call last):
File "c:\Users\climia1\Downloads\hangman.py", line 3, in <module>
r = requests.get('https://api.hypixel.net/skyblock/bazaar?key=29d9ecf6-71bb-49b1-b931-e0ed5f00f31d').json()
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\climia1\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\climia1\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\climia1\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)
[Done] exited with code=1 in 18.946 seconds
it displays the same error on repl.it but it goes away after one run. with visual studio it stays. any help?
Your json file must be empty or isn't formatted properly
if you are reading or writing to a empty Json file make sure it contains {}
if you are writing or reading a json file with lot of data make sure all the tags are closed
i have this code here:
import json
with open("pass_file.txt", "r") as file:
password = json.loads(file.read())
it calls this error:
Traceback (most recent call last):
File "testdoc.py", line 9, in <module>
print(json.loads(file.read()))
File "C:\Program Files\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Program Files\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\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 dont know why this is happening because i have the same code on another file just with different variable name and file name and it works file. I did notice another similar question about a similar error but it didnt answer my question.
Thanks in advance :)
What is the content of you pass_file.txt ? The python code use json.loads so it expect JSON formated content in the pass_file.txt
For example for a string, the content of this file will be "hello world"
If you don't put quotes, the JSON parsing process will fail.
I am having JSON syntax issues when I am using the following code code: https://github.com/clarkbk/streeteasy-analysis
Using this JSON in buildings.json
{
"buildings": [
{
"name": "Henry Hall",
"addr": "https://streeteasy.com/nyc/property_activity/past_transactions_component/799324?all_activity=true&show_rentals=true&style=xls",
"id": 799324,
}
]
}
I am getting the following error:
2019-05-25 16:04:26,641 - INFO - Starting...
Traceback (most recent call last):
File "run.py", line 27, in <module>
data = json.load(f)
File "/usr/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
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 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 9 column 5 (char 220)
root#LAPTOP-4QGC19OR:/home/HN/streeteasy-analysis#
I have researched for a few hours now on how to fix this but can't come up with a fix. I am not that familiar with JSON in general but I don't know where I am not double qouting properly. Appreciate any help with this.
The line number gives a good hint
you want:
"id": 799324
}
(note no comma after the last element)
json isn't python ast.literal_eval, if there's a comma on last element it fails, because it expects another property as the message states (Expecting property name enclosed in double quotes explains that, although the message could be better, as this error is very common)
If you have data like this, you can use ast.literal_eval on it instead, it will work without modifications (unless there are false or null json booleans/null-pointers)
This file has worked every time I've run it every day until today. Based on the error, here's what I think is the relevant code:
LastFM_artistMBID = MusicBrainz_artistMBID
get_artist_info_from_LastFM = lastFM.makeGetArtistInfoFromLastFM_URL(LastFM_artistMBID)
artist_info_from_LastFM = requests.get(get_artist_info_from_LastFM)
artistData = json.loads(artist_info_from_LastFM.text)
That last line is what the error(s) at the bottom seem to hate.
The MusicBrainz_artistMBID variable in the first line above is an item from the mbid_array2 list below. All of the above code is part of the get_artists_data function below. If you need more code, I can edit this answer.
for mbid in artistsData.mbid_array2:
get_artists_data(mbid)
Errors (including the first line from a print command while debugging):
<Response [200]>
Traceback (most recent call last):
File "getArtistDataCron.py", line 216, in <module>
get_artists_data(mbid)
File "getArtistDataCron.py", line 54, in get_artists_data
artistData = json.loads(artist_info_from_LastFM.text)
File "/home/bombybomb/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/home/bombybomb/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/bombybomb/anaconda3/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've tried several solutions and suggestions I've found from Googling but nothing seems to fix it. This file worked fine every time until today.
Expecting value: line 1 column 1 (char 0) means that the given value is entirely empty. Chances are the server is responding with no content at all due to a problem on the server end. You would just have to wait until the issues on the server are resolved if you have no access to fixing the server issues.
I am trying to pass the following JSON text into my python code.
{"platform": "android", "version": "6.0.1"}
My code is as follows.
import sys
import json
data = json.loads(sys.argv[1])
print(str(data))
When running the following on Windows 10 PowerShell,
python jsonTest.py '{"platform": "android", "version": "6.0.1"}'
I get the following:
Traceback (most recent call last):
File "jsonTest.py", line 3, in <module>
data = json.loads(sys.argv[1])
File "C:\Users\Rishabh Bhatnagar\AppData\Local\Programs\Python\Python36-
32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Rishabh Bhatnagar\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\Rishabh Bhatnagar\AppData\Local\Programs\Python\Python36-
32\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double
quotes: line 1 column 2 (char 1)
As far as I know, I take my code, and pass the JSON text properly. I can't figure out what I'm doing wrong. I know the JSON text is valid (checked with https://jsonlint.com/). Thanks.
So I figured it out.
sys.argv[1]
The above line was taking my Json text below and taking out the quotes from it.
{"platform": "android", "version": "6.0.1"}
into
{platform: android, version: 6.0.1}
My workaround is to run it as follows.
Python jsonTest.py '{\"platform\": \"android\", \"version\": \"6.0.1\"}'
I will try to find a better way, but for today, I'm done.
import sys
import json
data = json.loads(sys.argv[1].replace("'", '"'))
print(str(data))
This seems to work for me, python 3.6 when calling with python jsonTest.py "{'platform': 'android', 'version': '6.0.1'}"