Reputation: 113
I have some JSON file with the following contents :
{
"name": "",
"street": "",
"street_number": 26,
"district": "VILA MARESIA",
"city": "Raposa",
"state": "",
"country": "BR",
"latitude": ,
"longitude":
}
Now loading this file is throwing the error as below:
with open(r"C:\Users\dverma25\menus-folder\menus-folder-MA\rt.json",'r', encoding="utf8") as json_file:
json_data = json.load(json_file)
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-93-8150dd6a3e1d> in <module>
2 logger.info("Start processing the file {}".format(file))
3 # t = json_file.read()
----> 4 json_data = json.load(json_file)
C:\ProgramData\Anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
--> 296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297
298
C:\ProgramData\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
C:\ProgramData\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
C:\ProgramData\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 9 column 15 (char 153)
I got the reason behind this is value for the field "latitude" is missing which neither an empty string nor None(Because the datatype in source API is double for this field). Can anyone suggest a way to open the file and load it in json maodule.
PS : I also tried by opening the file and used josn.dump(file_obj.read()) but this has the issue that it's also taking all the carriage returns inside the dump().
Upvotes: 2
Views: 1958
Reputation: 101
some time you have ' instead " in the data file and you will get similar error as the one you got for example
import json
json_data = "{'key':'value'}"
json_obj = json.loads(json_data)
and to solve it just replace ' to " the data before you pass to the object
import json
json_data = "{'key':'value'}"
json_data_clean = json_data.replace("'","\"")
json_obj = json.loads(json_data_clean)
Upvotes: 0
Reputation: 11967
In your particular case you could load your invalid json string from the file then use a crude str.replace on the invalid entries setting them to null.
import json
my_json = """{
"name": "",
"street": "",
"street_number": 26,
"district": "VILA MARESIA",
"city": "Raposa",
"state": "",
"country": "BR",
"latitude": ,
"longitude":
}"""
my_json = my_json.replace(": ,", ": null,").replace(": \n", ": null\n")
print(json.loads(my_json))
OUTPUT
{'name': '', 'street': '', 'street_number': 26, 'district': 'VILA MARESIA', 'city': 'Raposa', 'state': '', 'country': 'BR', 'latitude': None, 'longitude': None}
Upvotes: 3
Reputation: 11922
Your file isn't properly formatted JSON. Specifically these lines:
"latitude": ,
"longitude":
They look like key-value pairs of that dictionary without the value part. They should look like this:
"latitude": "",
"longitude": ""
Upvotes: 2