I'm a newbie into Python and i'm trying to open a json file that looks like this:
{
"empty_char":"",
"empty_number": ,
"char":"i'm a char",
"number":0
}
Let's say that this is the test.json file:
When trying to open it in python I'm using:
import json
with open('test.json') as f:
data = json.load(f)
and it raises the following error:
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "C:\ProgramData\Anaconda3\Lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\ProgramData\Anaconda3\Lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\ProgramData\Anaconda3\Lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\ProgramData\Anaconda3\Lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 3 column 19 (char 39)
It gives an error because of "empty_number": ,
How can I deal with this error if I can't modify my .json file?
You can use the more flexible yaml to read in values not provided as None.
import yaml
from io import StringIO
mystr = StringIO("""{
"empty_char":"",
"empty_number": ,
"char":"i'm a char",
"number":0
}""")
# replace mystr with open('test.json', 'r')
with mystr as stream:
res = yaml.load(stream)
print(res, type(res))
{'empty_char': '', 'empty_number': None, 'char': "i'm a char", 'number': 0}
<class 'dict'>
Note io.StringIO lets us read from a string as if it were a file.
Related
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)
I want to read a JSON file with Python :
Here is part of my JSON file :
{ "Jointure":[ { "IDJointure":1, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:47:00"}, { "IDJointure":2, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:50:00"}]}
This is the code :
import json
data = json.loads('Data2019.json')
for i in data['Jointure']:
print(i)
But, here is the error that was displayed
Traceback (most recent call last):
File "C:\Users\HP\Desktop\readJSON.py", line 4, in <module>
data = json.loads('Data2019.json')
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\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.loads() expects the json data to already be a string -- so it's trying to interpret the filename Data2019.json as actual json data.
Open the file, and then pass the file object to json.load():
with open('Data2019.json') as fp:
data = json.load(fp)
don't read the file directly. Open the file it's only the contents of the file that works with the json module. Try this:
import json
with open('path_to_file/person.json') as f:
data = json.load(f)
Try pandas
import pandas as pd
patients_df = pd.read_json('E:/datasets/patients.json')
patients_df.head()
I am streaming data from a sensor using socket library in Json format, and trying to parse it and load it into Database.
When I print the stream I get this Json in this format:
b'[{"metadata":{"timezone":{"location":"Etc/UTC"},"serial_number":"00:07:32:52:09:fc","device_type":"SPIDER"},"timestamp":"2019-08-29T13:53:05.895Z","framenumber":"2290718","tracked_objects":[{"id":2592,"is_at_border":true,"type":"PERSON","position":{"x":233,"y":262,"type":"FOOT","coordinate_system":"PROCESSING_IN_PIXEL"},"person_data":{"height":1728}}]}]'
Based on my research prefix b stands for the byte type. So when I try to parse it with code below:
while True:
message, address = server_socket.recvfrom(1024)
message = message.upper()
# loading json file.
objs_json = json.loads(message)
# using if looop to prevent script of trying to to parse data without any object being tracked.
if "tracked_objects" in objs_json:
# Parsing json file with json_normalize object
objs_df = json_normalize(
objs_json, record_path='tracked_objects',
meta=[['metadata', 'serial_number'], 'timestamp']
)
# Renaming columns
objs_df = objs_df.rename(
columns={
"id": "object_id", "position.x": "x_pos",
"position.y": "y_pos", "person_data.height": "height",
"metadata.serial_number": "serial_number",
"timestamp": "timestamp"
}
)
# Selecting columns of interest
objs_df = objs_df.loc[:, ["timestamp", "serial_number", "object_id", "x_pos", "y_pos", "height"]]
# Writting the data into SQlite db
objs_df.to_sql('data_object', con=engine, if_exists='append', index=False)
# In case there is no tracks, print No Tracks in console.
else:
print("No Tracks")
I get this error message:
Traceback (most recent call last):
File "/home/pi/ProRail-PMS/Test_Spider2.py", line 20, in <module>
objs_json = json.loads(message)
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 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 215 (char 214)
However if I save that data into json file and remove the prefix b my parsing code works.
How do i go around this so when I receive the data from socket library I want to be able to parse it and feed it into database?
At first I wanted to comment that it works for me, but then I noticed how you get the message and what you do with it:
Remove message = message.upper():
>>> message = b'[{"metadata":{"timezone":{"location":"Etc/UTC"},"serial_number":"00:07:32:52:09:fc","device_type":"SPIDER"},"timestamp":"2019-08-29T13:53:05.895Z","framenumber":"2290718","tracked_objects":[{"id":2592,"is_at_border":true,"type":"PERSON","position":{"x":233,"y":262,"type":"FOOT","coordinate_system":"PROCESSING_IN_PIXEL"},"person_data":{"height":1728}}]}]'
>>> json.loads(message)
[{'metadata': {'timezone': {'location': 'Etc/UTC'}, 'serial_number': '00:07:32:52:09:fc', 'device_type': 'SPIDER'}, 'timestamp': '2019-08-29T13:53:05.895Z', 'framenumber': '2290718', 'tracked_objects': [{'id': 2592, 'is_at_border': True, 'type': 'PERSON', 'position': {'x': 233, 'y': 262, 'type': 'FOOT', 'coordinate_system': 'PROCESSING_IN_PIXEL'}, 'person_data': {'height': 1728}}]}]
>>
>>
>>> message = message.upper()
b'[{"METADATA":{"TIMEZONE":{"LOCATION":"ETC/UTC"},"SERIAL_NUMBER":"00:07:32:52:09:FC","DEVICE_TYPE":"SPIDER"},"TIMESTAMP":"2019-08-29T13:53:05.895Z","FRAMENUMBER":"2290718","TRACKED_OBJECTS":[{"ID":2592,"IS_AT_BORDER":TRUE,"TYPE":"PERSON","POSITION":{"X":233,"Y":262,"TYPE":"FOOT","COORDINATE_SYSTEM":"PROCESSING_IN_PIXEL"},"PERSON_DATA":{"HEIGHT":1728}}]}]'
>>> json.loads(message)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 217 (char 216)
>>> message[217:]
b'RUE,"TYPE":"PERSON","POSITION":{"X":233,"Y":262,"TYPE":"FOOT","COORDINATE_SYSTEM":"PROCESSING_IN_PIXEL"},"PERSON_DATA":{"HEIGHT":1728}}]}]'
Your upper breaks the True value that is unquoted (because it's a boolean, not a string). ;)
Imported it into Notepad++ since the JSON file is of type byte. Saved it out as JSON and it saved normally as a standard JSON file. Used Python json.load(f) to bring it in from my drive and then it can be used.
hi I want to read a JSON file in python and any kind of syntax that i use i receive an error and i don't know what to do the code :
import urllib
import pprint
import json
import re
import requests
import pprint
with open('synsets.json' , encoding = 'utf-8') as json_data:
d = json.loads(json_data.read())
json_data.close()
pprint(d)
the error :
Traceback (most recent call last):
File "D:\markaz\wikiomega\code1.py", line 13, in <module>
d = json.loads(json_data.read())
File "C:\Users\BehnaM1\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\BehnaM1\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\BehnaM1\AppData\Local\Programs\Python\Python35-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)
[Finished in 0.7s with exit code 1]
I have a json string in a txt file and I'm trying to read it to do some other procedures afterwards. It looks like this:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.load(f)
I know the json is valid, but I'm getting:
Traceback (most recent call last):
File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
user_input()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
child_remover()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
x = json.load(f)
File "C:\Python33\lib\json\__init__.py", line 274, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Python33\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python33\lib\json\decoder.py", line 370, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I used this website to check if the string is valid. If I use .loads(), I get a different error:
Traceback (most recent call last):
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
user_input()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
child_remover()
File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
x = json.loads(f)
File "C:\Python33\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
Originally the json was embeded in my script like this:
json_text="""json stuff here"""
And didn't get any errors. Any ideas on how to fix this???
Running python 3.3.3 just in case.
Thanks!!
EDIT:
Just some random (valid) json on the txt and I get the same issue. This os one of the ones i tried:
{"data":
{"mobileHelp":
{"value":
{
"ID1":{"children": [1,2,3,4,5]},
"ID2":{"children": []},
"ID3":{"children": [6,7,8,9,10]}
}
}
}
}
Which is valid as well as per jsonlint.com.
Your file contains a UTF-8 BOM character at the start. UTF-8 doesn't need a BOM but especially Microsoft tools insist on adding one anyway.
Open the file with the utf-8-sig encoding instead:
>>> open('/tmp/json.test', 'wb').write(b'\xef\xbb\xbf{"data":\r\n {"mobileHelp":\r\n {"value":\r\n {\r\n "ID1":{"children": [1,2,3,4,5]},\r\n "ID2":{"children": []},\r\n "ID3":{"children": [6,7,8,9,10]}\r\n }\r\n }\r\n }\r\n}')
230
>>> import json
>>> with open('/tmp/json.test', encoding='utf8') as f:
... data = json.load(f)
...
Traceback (most recent call last):
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 271, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 369, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> with open('/tmp/json.test', encoding='utf-8-sig') as f:
... data = json.load(f)
...
>>> data
{'data': {'mobileHelp': {'value': {'ID2': {'children': []}, 'ID3': {'children': [6, 7, 8, 9, 10]}, 'ID1': {'children': [1, 2, 3, 4, 5]}}}}}
Note that from Python 3.4 onwards you get a more helpful error message here:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 314, in loads
raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
ValueError: Unexpected UTF-8 BOM (decode using utf-8-sig)
Not sure what your code looks like for the second error, but it looks like you are passing json.loads a file object and not a string. Try:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.loads(f.read())
or without newlines with:
with open('code test.txt', 'r', encoding=('UTF-8')) as f:
x = json.loads(f.read().replace('\n', ''))
As another choice, This will be much easier to fix this issue.
json.loads(open('test.txt').read().decode('utf-8-sig'))