Reading and parsing a json file - python

I'm using the following python script to read and parse a json file
import json
with open('testdata.json', 'r') as raw_data:
content = json.load(raw_data)
print(content)
that has data like:
{"grp":"1"; "total":"10"}
{"event":"run", "timestamp":"2010-01-30 10:00:40", "id": "200", "distance": "5"}
{"event":"walk", "timestamp":"2010-01-31 18:46:00", "id": "200", "disrance": "2"}
I'm getting the error:
Traceback (most recent call last):
File "readdata.py", line 4, in <module>
content = json.load(raw_data)
File "/usr/lib/python2.7/json/__init__.py", line 290, in load **kw)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 93 - 187)
If I have one row of data it works... 2 or more rows of data I get the error
Can't see anything that is causing this problem

The SO syntax highlighter solved your issue.
"distance': "5"}
^
Change this to double quotes
But there are many other issues. here is a valid version of your json file.
[
{"grp":1, "total":10},
{"event":"run", "timestamp":"2010-01-30 10:00:40", "id": "200", "distance": "5"},
{"event":"walk", "timestamp":"2010-01-31 18:46:00", "id": "200", "disrance": "2"}
]
Note the " arround each key. the , between key:value pairs, and the , between elements of the list.
You can validate your JSON using tools like jsonlint.com

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

Unable to read a JSON file in my simple python code

I get the below error when i try to read a JSON file, not sure what needs to be corrected:
C:\Users\prabhjot2600\PycharmProjects\PythonPractise\venv\Scripts\python.exe C:/Users/prabhjot2600/Desktop/Prabh/alien_invasion/test.py
Traceback (most recent call last):
File "C:/Users/prabhjot2600/Desktop/Prabh/alien_invasion/test.py", line 26, in <module>
data = json.loads(people_string)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 11 column 21 (char 192)
My code is simply reading a JSON file and printing its data. I have checked my JSON data also which looks correct and also formatted it to see if there were any errors left but still cant read the JSON
import json
people_string = '''
{
"people": [
{
"name": "Prabhjot Singh",
"phone": "9999596310",
"emails": [
"prabh#noemail.com",
"prabhjot#ranamail.com"
],
"hasLicence": True
},
{
"name": "Sunny Rana",
"phone": "9999988888",
"emails": null,
"hasLicence": False
}
]
}
'''
data = json.loads(people_string)
print(data)
JSON stands for JavaScript Object Notation.
So it is subset of Javascript's syntax.
Javascript uses true and false keyword as literal boolean values.
(Whereas python uses True and False).
Therefore, you should use true and false instead of True and False.
See https://www.json.org/json-en.html for detailed syntax.

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()

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

Categories

Resources