How to solve problem during reading of a json file in Python? - python

I`m currently programming an automated wateringsystem using Python on a Raspberry Pi. For that I want to read the content of a json file, with the following code:
import logging
import json
try:
with open('/var/www/BWSWebApp/apictx.json') as file:
jsondata = json.load(file)
except FileNotFoundError:
print('Error: File not found (ln 35)')
_logging = jsondata['main']['logging']
if _logging == True:
_logging = '/home/pi/Desktop/BewaesserungssystemV3/data/info_log.log'
else:
_logging = None
The json file is containing the following:
{
"main": {
"active": true,
"watering": false,
"fwatering": false,
"ffminwatering": false,
"humidity": 24,
"temp": 17.6,
"battery": 100,
"logging": true,
"last_watering": "27.08.2021 16:15:39",
"l_day": 0,
"day": 0,
}
}
If I run the program, I get the following error:
Traceback (most recent call last):
File "/home/pi/Desktop/BewaesserungssystemV3/main.py", line 6, in <module>
jsondata = json.load(file)
File "/usr/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 14 column 9 (char 316)
What am I doing wrong?
Thanks, Jannis

You need to remove the comma after the last value ie day: 0.
You can use JSON online validator if you encounter problems like these in the future.
{
"main": {
"active": true,
"watering": false,
"fwatering": false,
"ffminwatering": false,
"humidity": 24,
"temp": 17.6,
"battery": 100,
"logging": true,
"last_watering": "27.08.2021 16:15:39",
"l_day": 0,
"day": 0
}
}

Related

builtins.StopIteration: 2 and json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2) errors

I have a JSON file I am trying to import into MongoDB.
Using the python code from the [documentation][1], I get an error on myDict = json.loads(jsonObj) (see traceback below). I have used an online tool to validate my JSON and it is marked as valid. The problem seems to be in line 2 but I have no idea what to do to make this work. Please help!
Python traceback:
obj, end = self.scan_once(s, idx)
builtins.StopIteration: 2
During handling of the above exception, another exception occurred:
File "/Users/s/Documents/s/importToMongo.py", line 13, in <module>
myDict = json.loads(jsonObj)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)```
**My JSON:**
```[
{
"field1": "6405441",
"field9": "326",
"field10": "12",
"field17": "1180",
"field22": "Avenue Brugmann",
"field23": "Brugmannlaan",
"field24": null,
"field28": "Uccle",
"field29": "Ukkel",
"field30": null,
"field13": "648301.355 666355.729"
},
{
"field1": "fid",
"field9": "housenumber",
"field10": "boxnumber",
"field17": "postal_info_objectid",
"field22": "streetname_fr",
"field23": "streetname_nl",
"field24": "streetname_de",
"field28": "municipality_fr",
"field29": "municipality_nl",
"field30": "municipality_de",
"field13": "position"
}
]```
[1]: https://www.mongodb.com/compatibility/json-to-mongodb
Problem was this code only works for JSON objects with this format:
No brackets ( [] )
No commas at the end of the last field-value pair
Only one object
Example:
{
"field1":"value",
"field2":"value"
}

Why am I getting a JSONDecodeError when trying to load a JSON file in Python?

I'm trying to load a JSON file in to Python, but it's giving me an JSONDecodeError error which seems to suggest the file is empty... which I know is not true.
I've reviewed similar questions I can find on here (65466227 & 62286816), and while one or two provide useful alternatives... that's not really the point. I feel like what I'm doing should work but it doesn't.
I put my JSON in to jsonlint.com and it confirmed that my JSON is indeed valid.
Error message/traceback:
Traceback (most recent call last):
File "utils/fmt_test.py", line 62, in <module>
test_cases_json = json.load(jf)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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)
My Python code:
test_case_file, _ = os.path.splitext(fmt_file)
test_case_file = f"{test_case_file}.json"
if os.path.isfile(test_case_file):
with open(test_case_file, "r") as jf:
test_cases_json = json.load(jf)
My JSON:
{
"tests": [
{
"id": "746fd83s23dy",
"event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING TEST LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST2 LFA_DUMMYSTRING",
"match": "True"
},
{
"id": "990gb98s34dm",
"event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
"match": "True"
},
{
"id": "100ma09x31ui",
"event": "%t localhost LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST3 LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
"match": "True"
}
]
}
Any help is much appreciated, thanks.
There is likely a UTF-8 BOM encoded at the beginning of the file since it is complaining about the first byte. Open with encoding='utf-8-sig' and it will be removed if present:
>>> import json
>>> data = {}
>>> with open('test.json','w',encoding='utf-8-sig') as f: # write data with BOM
... json.dump(data,f)
...
>>> with open('test.json') as f: # read it back
... data2 = json.load(f)
...
Traceback (most recent call last):
<traceback removed>
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) # same error
>>> with open('test.json',encoding='utf-8-sig') as f:
... data2 = json.load(f)
...
>>> # worked

Read json file get error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I try to read data from my json file. First I convert my xml file to json and I get in output this json file for example :
[
{
"ABC": {
"idObjet": "AffleurantEnveloppePCRS.0",
"nature": "03",
"precisionPlanimetrique": "010",
},
"reseau": "DECH",
"thematique": "10"
},
"xmlns:fn": "http://www.w3.org/2005/xpath-functions",
},
{
"DEF": {
"enveloppe": {
"xlink:href": "GEOMETRIE.ENV.AffleurantEnveloppePCRS.0"
},
"gml:id": "GEOMETRIE.AffleurantEnveloppePCRS.0"
},
"xmlns:fn": "http://www.w3.org/2005/xpath-functions",
}
]
Then I try to print some data from this file. I use json.loads() to read this data:
import xmltodict
import json
dict = xmltodict.parse(open('Testv2.gml').read(), attr_prefix='')
dict3 = json.loads('test.json', strict=False)
print(dict3.keys())
with open('test.json', "wt", encoding='utf-8', errors='ignore') as f:
json.dump(dict, f, indent=4, sort_keys=True)
I try to print only keys (ABC and DEF)
ERROR :
Traceback (most recent call last):
File "C:/test.py", line 7, in <module>
dict3 = json.loads('test.json', strict=False)
File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 370, in loads
return cls(**kw).decode(s)
File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\AppData\Local\Programs\Python\Python38-32\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)

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) While using JSON package to parse JSON

Im trying to change a value in a JSON file, but it gives me an error when I run it, and I cant figure out why. Heres my error:
Traceback (most recent call last):
File "C:\Users\Kyle\Desktop\BeatSaber.jl-master\src\main.py", line 4, in <module>
data = json.load(f)
File "C:\Users\Kyle\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\Kyle\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\Kyle\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Kyle\AppData\Local\Programs\Python\Python38-32\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)
And Heres my code:
import json
with open("data.json", "r") as f:
data = json.load(f)
data["_songAuthorName"] = "jfosenoi"
with open("data.json", "w") as f:
data = json.dump(f)
My JSON is created by a program that I made that's not Python, but here it is:
{
"_version": "2.0.0",
"_songName": "<SongName>",
"_songSubName": "",
"_songAuthorName": "",
"_levelAuthorName": "Kyle Buchanan",
"_beatsPerMinute": 60,
"_songTimeOffset": 0,
"_shuffle": 0,
"_shufflePeriod": 0,
"_previewStartTime": 2,
"_previewDuration": 0,
"_songFilename": "song.egg",
"_coverImageFilename": "cover.jpg",
"_environmentName": "DefaultEnvironment",
"_difficultyBeatmapSets": [
{
"_beatmapCharacteristicName": "Standard",
"_difficultyBeatmaps": [
{
"_difficulty": "ExpertPlus",
"_difficultyRank": 9,
"_beatmapFilename": "ExpertPlus.dat",
"_noteJumpMovementSpeed": 0,
"_noteJumpStartBeatOffset": 0
}
]
}
]
}
with open("data.json", "w") as f:
data = json.dump(f)
This is wrong,
Try this:
with open("data.json", "w") as f:
json.dump(data, f)
Docs: https://docs.python.org/3/library/json.html#json.dump

json.loads() - json.decoder.JSONDecodeError: Expecting value

I should read a json file located in the same folder as the python file.
The code is this:
import json
import os
with open(os.path.join(os.path.dirname(__file__), 'datasets.json'), 'r') as f:
dataset = json.loads(f.read())
This is the error:
Traceback (most recent call last):
File "Desktop/proj/ai/index.py", line 6, in <module>
dataset = json.loads(f.read())
File "/opt/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/opt/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 14 column 1 (char 284)
This is the JSON:
[
{
"name": "linear1",
"values": [[1,3],[2,5],[3,7]]
},
{
"name": "linear2",
"values": [[1.1,2],[2.1,4.3],[2.9,6.4],[4.1,7.9],[5.2,9.7],[6.4,12],[6.5,13.3],[8,15.9],[8.9,18.1],[9.7,20.4]]
},
{
"name": "parabolic1",
"values": [[1,1],[2,4],[3,9]]
},
]
You JSON is incorrect,See where i have removed a comma in the JSON below
[
{
"name": "linear1",
"values": [[1,3],[2,5],[3,7]]
},
{
"name": "linear2",
"values": [[1.1,2],[2.1,4.3],[2.9,6.4],[4.1,7.9],[5.2,9.7],[6.4,12],[6.5,13.3],[8,15.9],[8.9,18.1],[9.7,20.4]]
},
{
"name": "parabolic1",
"values": [[1,1],[2,4],[3,9]]
} < ---- Removed This Comma
]
What is the contents of your json file? It is probably some incorrect formatting in there. There are various json validators online like https://jsonlint.com/ which can help check issues with these.

Categories