Problem in parsing data of a JSON file with Python - 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"
]
}
]
}
]
}

Related

Python: why does repeat of json.load cause error [duplicate]

This question already has answers here:
Why can't I call read() twice on an open file?
(7 answers)
Closed last year.
with open(LATEST_UPDATE_FULL_PATH) as JSON_FILE_UPDATE:
JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)
JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)
why does doing "load" twice causes the following below?
Is there a handle on this file? I could not find anything to release JSON_FILE_UPDATE.
Traceback (most recent call last):
File "PhthonScript", line 69, in <module>
JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)
File "...\PythonByMiniconda3\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "...\PythonByMiniconda3\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "...\PythonByMiniconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "...\PythonByMiniconda3\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)
try this:
JSON_DATA_UPDATE = json.loads(JSON_FILE_UPDATE)
once it is loaded, then the file converted to json. cannot convert json to json.
another way, this may work on string to json:
import ast
file = """{
"students": [
{
"name": "Millie Brown",
"active": False,
"rollno": 11
},
{
"name": "Sadie Sink",
"active": True,
"rollno": 10
}
]
}"""
print(type(file))
dict = ast.literal_eval(file)
print(type(dict))
dict['students'][0]['name'] would be "Millie Brown"
dict['students'][1]['active'] would be True

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

self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ','

I have a large json file (2.4 GB). I want to parse it in python. The data looks like the following:
[
{
"host": "a.com",
"ip": "1.2.2.3",
"port": 8
},
{
"host": "b.com",
"ip": "2.5.0.4",
"port": 3
},
{
"host": "c.com",
"ip": "9.17.6.7",
"port": 4
}
]
I run this python script parser.py to load the data for parsing::
import json
from pprint import pprint
with open('mydata.json') as f:
data = json.load(f)
Previously, I made this post about the same code. I am trying to run the code with larger RAM. but I got a different error. Can you please help me identify the source of the problem?
Traceback (most recent call last): File "parser.py", line 6, in
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 ',' delimiter: line 1095583 column 749 (char 56649111)
There is a similar problem in this post but I could not use the solution as I read my json array from a file. Not sure how to apply the solution in this case?

Python: SyntaxError: unexpected EOF while parsing

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

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