Python: SyntaxError: unexpected EOF while parsing - python

I am trying to read a dictionary object from an external file and then read it from another file. (The object inside of the file apparently is not a json file even though the file name has a json extension.)
import json
import ast
with open('remeeting_media-get-response.json', 'r') as data:
s = data.read()
a = ast.literal_eval(s)
type(a)
However, I am getting the following unknown error:
Traceback (most recent call last):
File "/Users/me/Desktop/data/finished/Dashlane/diarization.py", line 8, in <module>
a = ast.literal_eval(s)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 0
^
SyntaxError: unexpected EOF while parsing
[Finished in 0.1s with exit code 1]
This is the sample content from the file:
{
"lines": [
{
"duration": 1.8899999999999999,
"line": "these cop ooh",
"interval": [
0.0,
1.8899999999999999
],
"speaker": "Speaker_2"
},
{
"duration": 5.9500000000000002,
"line": "[noise] hello [noise]",
"interval": [
2.3199999999999998,
8.2699999999999996
],
"speaker": "Speaker_1"
},
{
"duration": 1.5600000000000001,
"line": "ooh",
"interval": [
2081.6900000000001,
2083.25
],
"speaker": "Speaker_2"
}
]
}
I also tried to load this as a json object and it doesn't recognize it as json.
import json
import ast
with open('remeeting_media-get-response.json', 'r') as data:
raw = json.load(data)
print raw
See the output:
Traceback (most recent call last):
File "/Users/me/Desktop/data/finished/Dashlane/diarization.py", line 7, in <module>
raw = json.load(data)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/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

Related

having trouble understanding how to load json and iterate through it

I have the following JSON document:
[
{
"iLevel": 85,
"isEthereal": true,
"quality": "Normal",
"stats": [
{
"name": "item_splashonhit",
"value": 100
}
],
"type": "Legendary Mallet"
},
{
"defense": 720,
"iLevel": 88,
"isEthereal": true,
"quality": "Normal",
"sockets": 4,
"type": "Diamond Mail"
},
{
"defense": 732,
"iLevel": 69,
"isEthereal": true,
"quality": "Normal",
"type": "Boneweave"
}
]
Here is the code I've put together so far and the error. I think that it is perhaps the JSON format which is wrong?
# packages
import json
from json import JSONEncoder
from pathlib import Path
from typing import Collection
# get all json files in 'json' folder
folderPath = '.\\json\\'
jsonFiles = (f for f in Path(folderPath).glob("*") if f.is_file())
for j in jsonFiles:
fileCont = open(j.resolve())
for f in fileCont:
obj = json.loads(f)
print(obj.name, obj.quality, obj.ilevel, obj.type)
fileCont.close()
I also don't think I am using the 'json' library correctly. Sorry I am still very new to python.
Error:
Traceback (most recent call last):
File "C:\Users\username\script.py", line 17, in <module>
obj = json.loads(d)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\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 3)
Any help is appreciated. Once I get through this I'll be looking to iterate through the 'stats' object as well but I'll cross that bridge later. I just want 'print()' to output the values so I know I am working with objects correctly.
The is one subtle trick, which you should pay attention for and thats the load functionality of json :
json.loads() - should be used for a string-like files
json.load() - should be used for a file-like objects
So if i was you, I would start like this :
for j in jsonFiles:
with open(j, "r") as f:
t = json.load(f)
for i in t:
i.get("stats") # returns None if nothing found

Problem in parsing data of a JSON file with Python

This is my ciao.json that i need to parse some data from it
{
"opcua": [
{
"ip": ciao,
"port": 4840,
"uri": "http://examples.freeopcua.github.io"}
"objects":[
{
object_name: ListaDeiSensori,
variables:[
"Temperature_Sensor",
"Water_Sensor"]
}]
}]
}
To pase the fileds of the json file i am using this python script:
import json
with open('ciao.json') as file:
data = json.load(file)
print(data
But i get this error:
Traceback (most recent call last):
File "getdata.py", line 11, in <module>
data = json.load(file)
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 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 4 column 28 (char 72)
Is there anyone that can help me in solving this problem?
I do not know how to solve it...
I think there are some errors in the json file.
update json, I changed undefined obj to string.
{
"opcua": [
{
"ip": "ciao",
"port": 4840,
"uri": "http://examples.freeopcua.github.io"
},
{
"objects": [
{
"object_name": "ListaDeiSensori",
"variables": [
"Temperature_Sensor",
"Water_Sensor"
]
}
]
}
]
}

how to open json file in python

I am stuck here again... I have a file named "data.json" and I want to open it with python but I am getting errors.
import json
>>> data=json.load(open("data.json"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\Angel\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 340,
in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 4912995)
>>>
According to Python JSON documentation
If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.
Not knowing the content of your file, it is hard to say what is wrong, but I would suspect that text in your file is not a valid JSON object, or more likely (according to "Extra data" search, answered here) the file "data.json" includes more than one JSON object.
For example, using your code:
This file works correctly
{ "name":"John", "age":30, "car":null }
but this one
{ "name":"John", "age":30, "car":null }
{ "name":"John", "age":30, "car":null }
throws the same errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py",
line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py",
line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py",
line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 6 column 1 (char 55)
In case 2 or more than 2 record, you have to reformat your file as mentioned below OR you have to load file record by record.
You need to reformat your json to contain an array like below:
{
"foo" : [
{"name": "XYZ", "address": "54.7168,94.0215", "country_of_residence": "PQR", "countries": "LMN;PQRST", "date": "28-AUG-2008", "type": null},
{"name": "OLMS", "address": null, "country_of_residence": null, "countries": "Not identified;No", "date": "23-FEB-2017", "type": null}
]
}

appending json files to text file

I have a list, where each element is path to json file. i am using following code to write into a text file:
list=['C:/Users/JAYANTH/Desktop/datasets/580317556516483072/source-
tweet/580317556516483072.json',
'C:/Users/JAYANTH/Desktop/datasets/580317998147325952/source-
tweet/580317998147325952.json',
..........]
file=open("abc.txt","a")
for i in range(len(list))
with open(list[i]) as f:
u=json.load(f)
s=json.dumps(u)
file.write(s)
file.close()
this code appends data in json files to txt file. While trying to read the data from same file using following code:
with open('abc.txt','r') as f:
for each in f:
print(json.load(file))
I am getting the following error:
Traceback (most recent call last):
File "C:\Users\JAYANTH\Desktop\proj\apr25.py", line 15, in <module>
print(json.load(file))
File "C:\Users\JAYANTH\Desktop\proj\lib\json\__init__.py", line 268, in
load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
**kw)
File "C:\Users\JAYANTH\Desktop\proj\lib\json\__init__.py", line 319, in
loads
return _default_decoder.decode(s)
File "C:\Users\JAYANTH\Desktop\proj\lib\json\decoder.py", line 339, in
decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\JAYANTH\Desktop\proj\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)
I have also tried using json.loads, but getting an error:
Traceback (most recent call last):
File "C:\Users\JAYANTH\Desktop\proj\apr25.py", line 15, in <module>
print(json.loads(file))
File "C:\Users\JAYANTH\Desktop\proj\lib\json\__init__.py", line 312, in
loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'TextIOWrapper'
How can I solve this issue?
A concatenation of json is not json, full stop. This file is a valid Json:
[ 1, 2 ,
{ "a": "b" },
3 ]
and give this Python object: [1, 2, {'a': 'b'}, 3]
But this is no longer a valid json file:
[ 1, 2 ,
{ "a": "b" },
3 ]
[ 1, 2 ,
{ "a": "b" },
3 ]
because it contains 2 object with no relations.
You should enclose everything in an outer list to make it valid:
[
[ 1, 2 ,
{ "a": "b" },
3 ]
,
[ 1, 2 ,
{ "a": "b" },
3 ]
]
So you generating code should be:
file=open("abc.txt","a")
file.write("[\n") # start of a list
first = True
for i in range(len(list))
if first:
first = False
else:
file.write(",\n") # add a separating comma before every element but the first one
with open(list[i]) as f:
u=json.load(f)
s=json.dumps(u)
file.write(s)
file.write("]\n") # terminate the list
file.close()

JSON parsing in python gives error - UnicodeDecodeError

My JSON file is
{ "results": [
{ "ID": "63768E9B-1D66-486A-BCDD-D3991EAFBE94",
"Dt": "2013-08-03T13:01:26.901Z",
"Dt_u": "2013-08-03T13:01:26.901Z",
"obj": "enppXhI7TS"
},
{
"ID": "63768E9B-1D66-486A-BCDD-D3991EAFBE94",
"Dt": "2013-08-03T16:17:33.280Z",
"Dt_u": "2013-08-03T16:17:33.280Z",
"obj": "79J5z6y2UR"
},
{
"ID": "F8B1B9FB-7BCD-47DF-89BD-241440BB6270",
"Dt": "2013-08-06T00:23:43.562Z",
"obj": "Xf75BFtx4O",
"gender": 2,
"language": "en"
}]}
There are many more entries in the file
My python code is as follows
import json
from pprint import pprint
json_data=open('data.json','r')
data = json.load(json_data)
jsondata = data["results"]
for item in jsondata:
name = item.get("ID")
json_data.close()
When i try to run the file, it gives me following error
Traceback (most recent call last):
File "E:\test.py", line 7, in <module>
data = json.load(json_data)
File "C:\python27\lib\json\__init__.py", line 290, in load
**kw)
File "C:\python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\python27\lib\json\decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\python27\lib\json\decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 2: invalid continuation byte
[Finished in 0.2s with exit code 1]
Can somebody help me and tell how do i resolve it?

Categories

Resources