Reading a JSON file using Python - JSONDecodeError Extra Data - json

I'm following along with https://realpython.com/python-json/. I'm using Python 3.8, on a Windows 10 machine, using IDLE.
I deviated a bit from the example.
>>> import json
>>>
>>> data1 = {
'president': {
'name': 'dumb-bell beetlebox',
'species': 'betelgusian'
}
}
>>> data2 = {
'emperor': {
'name': 'Ezekiel Wheel',
'species': 'klingon'
}
}
>>> data3 = {
'king': {
'name': 'tech shiny',
'species': 'two hundred'
}
}
>>>
>>> with open('data1_file.json', 'w') as wf:
json.dump(data1, wf)
>>> with open('data1_file.json', 'a') as af:
af.write('\n')
json.dump(data2, af)
af.write('\n')
json.dump(data3, af)
1
1
This created the json file, with the data per line.
I then tried to read it.
>>> with open('data1_file.json', 'r') as rf:
data = json.load(rf)
Traceback (most recent call last):
File "<pyshell#139>", line 2, in <module>
data4 = json.load(rf)
File "D:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "D:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "D:\Program Files (x86)\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 73)
On the advice from a friend, who said there may have been extraneous data in the file -
>>> print(repr(open('data1_file.json').read()))
'{"president": {"name": "dumb-bell beetlebox", "species": "betelgusian"}}\n{"emperor": {"name": "Ezekiel Wheel", "species": "klingon"}}\n{"king": {"name": "tech shiny", "species": "two hundred"}}'
Any help would be appreciated. Thank you.

The problem is json.load does not decode multiple json objects. You'll probably want to place the data in an array. Check out this link for more info

Related

How can I fix this error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I am trying to create an REST API and a part of the project requires working with JSON files.
I am trying to open a JSON file, to check whether certain content exists in the file. If it doesn't, it will be added. For that reason, I am trying to load the JSON file in order to use the dictionary methods. When I open the file and try to use the json.loads() function, I get an error. I am attaching the part of the code that doesn't work.
file = open(self.storage_file_name, 'r')
if file_is_empty:
content_to_write = json.dumps(self.states_population_json, indent=4)
file.write(content_to_write)
else:
current_date = str(date.today())
print(file.read())
json_content = json.loads(file.read()) # THIS IS WHERE THE ISSUE APPEARS <<<
if current_date in self.states_population_json["features"].keys():
pass
else:
json_content["features"][current_date] = self.states_population_json["features"][current_date]
Here's the error as well:
Traceback (most recent call last):
File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\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)
I tried using json.load() instead of json.loads(), but then I get the following error:
Traceback (most recent call last):
File "C:\Users\denis\Desktop\Programming\ESRI_Internship\test_file.py", line 89, in <module>
data.save_data_json()
File "C:\Users\denis\Desktop\Programming\ESRI_Internship\test_file.py", line 79, in save_data_json
json_content = json.load(file)
File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\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)
Process finished with exit code 1
I made sure that the file isn't empty.
Thanks in advance!
EDIT:
Here is the content of the file (shortened a bit):
{
"features": {
"2022-01-06": [
{
"STATE_NAME": "Alabama",
"POPULATION": 5028316
},
{
"STATE_NAME": "North Carolina",
"POPULATION": 10736879
},
{
"STATE_NAME": "North Dakota",
"POPULATION": 321419
}
]
}
}
The line print(file.read()) moves the file cursor for reading to the end of the file (the command reads the whole file). A subsequent file.read() thus will not be able to read anything. Just remove the print statement, or if needed, move the file cursor back to the beginning of the file by executing file.seek(0) before the json.loads() statement.

Write the correct format of JSON to file using json.dumps() in Python 3

Let say I have a simple dictionary as below:
a = {'animal' : 'fish', 'fruit' : 'apple','vehicle' : 'car'}
I wish to convert a into JSON and write to a file. If I used json.dumps() as below:
b = jason.dumps(a)
with open('output.json', 'w') as f:
f.write(pprint.pformat(b))
The content of the JSON file output.json became:
'{"animal": "fish", "fruit": "apple", "vehicle": "car"}'
What I wish to actually have (in the file) is without quote as below:
{"animal": "fish", "fruit": "apple", "vehicle": "car"}
What is the right way to code it so that I can get the desired output?
Thanks in advance.
[EDIT]: The reason why I need the desired output in file is because I received the error below when I loaded the file.
>>> with open("output.json") as f:
... data = json.load(f)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Users/louis.kok/Analyses/devel/anaconda3/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/louis.kok/Analyses/devel/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/louis.kok/Analyses/devel/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/louis.kok/Analyses/devel/anaconda3/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 1 (char 0)
Import json library
import json
Save your file properly using json.dump
with open('output.json', 'w') as f:
json.dump(a, f)
Read it using json.load
with open("output.json") as f:
data = json.load(f)
Try this:
with open('task.json', 'w') as f:
s = json.dumps(mydict, indent=2)
f.write('%s\n' % (s))

How to skip text at the beginning of a json file in python

I have a json object file and at the beginning of this file a header of text, date, time, IP address.
I've tried f.readlines()[5:] to no avail.
I've tried next(f)
I wish to skip 5 or 6 lines of text and go directly into the json data.
Here is an example.
import jsonlines
import json
data_file = input("Enter a file to parse: ")
with jsonlines.open(data_file) as file:
for obj in file:
try:
jsonparse = json.loads(obj)
except Exception as e:
pass
print(obj)
Error:
jsonlines.jsonlines.InvalidLineError: line contains invalid json: Expecting value: line 1 column 1 (char 0) (line 1)
Top of json file:
Start: 07/02/2019 14:59:40.686
Connected To:
192.168.11.203
Here is the full long error:
Enter a file to parse: Play.raw
Traceback (most recent call last):
File "C:\Users\sdickey\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonlines\jsonlines.py", line 159, in read
value = self._loads(line)
File "C:\Users\sdickey\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\sdickey\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\sdickey\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)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/sdickey/PycharmProjects/Python Testing/Actall Data/testing.py", line 6, in <module>
for obj in file:
File "C:\Users\sdickey\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonlines\jsonlines.py", line 204, in iter
skip_empty=skip_empty)
File "C:\Users\sdickey\AppData\Local\Programs\Python\Python37\lib\site-packages\jsonlines\jsonlines.py", line 164, in read
six.raise_from(exc, orig_exc)
File "<string>", line 3, in raise_from
jsonlines.jsonlines.InvalidLineError: line contains invalid json: Expecting value: line 1 column 1 (char 0) (line 1)
Process finished with exit code 1
It is always preferable not to read the whole file in memory but one line at a time.
Assuming your input file contains:
first line
second line
third line
fourth line
fifth line
{ "k1": "val1", "k2": "val2" }
{ "k3": "val3", "k4": "val4" }
if you just want to skip 5 lines, you could do it brutally as:
import json
with open("test.txt") as f:
for _ in range(5):
next(f)
for line in f:
obj = json.loads(line)
print(obj)
or using enumerate:
import json
with open("test.txt") as f:
for i, line in enumerate(f):
if i<5:
continue
obj = json.loads(line)
print(obj)
or use itertools' dropwhile:
import itertools as it
import json
with open("test.txt") as f:
for i, line in it.dropwhile(lambda i: i[0]<5, enumerate(f)):
obj = json.loads(line)
print(obj)
I think you are trying to convert to json line by line. You need to join all lines by \n igoring the first 5 lines and then load it:
import json
with open("test.txt") as f:
json_obj = "\n".join(f.readlines()[5:])
jsonparse = json.loads(json_obj)
print(jsonparse)

Why am I getting error 'TypeError: string indices must be integers'

I have the JSON file below, and I am getting an error
Traceback (most recent call last):
File "test11.py", line 10, in <module>
print(driver['id'])
TypeError: string indices must be integers
{"drivers":
[
{
"id": "91907",
"groupId": "9039",
"vehicleId": "11111",
"currentVehicleId": "11111",
"username": "ablahblah",
"name": "Andrew Blahblah"
}
]
}
I have written the follow code to extract out values from the
file
import json
from pprint import pprint
with open('driver.json', 'r') as f:
drivers_dict = json.load(f)
for driver in drivers_dict:
print(driver['id'])
print(driver['groupId'])
print(driver['vehicleId'])
print(driver['username'])
print(driver['name'])
I need help to understand why I am getting the error and how to fix it.
Ultimately, the problem is that looping over a dict gives you the keys.
>>> [i for i in drivers_dict]
['drivers']
I think you just got your json layout confused. This works:
import json
with open('driver.json') as f:
j = json.load(f)
drivers_list = j["drivers"]
for driver in drivers_list:
# BTW you can DRY this part:
for key in ['id', 'groupId', 'vehicleId', 'username', 'name']:
print(driver[key])
Also Consider checking if the id is string or integer.
isinstance(s, str)

Hello Getting parsing json file

I need some help here parsing a json data :
My json File contain this
{
"message": "{\"gender\":\"MADAME\",\"Polo\":\"POTA\",\"last_name\":\"pato\",\"email\":\"pato.pota#mailler.com\",\"subject\":\"toto claim\",\"sub_subject\":\"Claim insurance car\",\"question\":\"claim for red car\",\"store\":\"claiming for incident\"}",
"context": [
],
"level": 200,
"level_name": "INFO",
"channel": "mailer",
"datetime": {
"date": "2016-09-19 11:00:26.795353",
"timezone_type": 3,
},
"extra": [
]
}
Python Code.
import os
import json
def Get_running_dir():
path = os.getcwd()
file = path + "\json_data.txt"
print(file)
with open(file, 'r') as f:
data = f.read()
data_json = json.loads(data)
print(data_json)
print(type(data_json))
Get_running_dir()
The issue is { print(type(data_json))} this is a dict right.
Once I call this print(data_json['message']['gender'])
<class 'dict'>
Traceback (most recent call last):
File "Extract_log.py", line 29, in <module>
Get_running_dir()
File "Extract_log.py", line 25, in Get_running_dir
print(data_json['message']['gender'])
TypeError: string indices must be integers
I need some help to parse this file please help me.
Thanking you in advance.
Regards,
I figured how to work with the json, this out today.
import os
import json
def Get_running_dir():
path = os.getcwd()
file = path + "\json_data.txt"
print(file)
with open(file, 'r') as f:
data = f.read()
data_json = json.loads(data)
# My error was here:
print(data_json['message']) # This convert to String.
msg = json.loads(data_json['message']) # THIS CONVERT THE STRING TO #Dict.
# this way i can access its keys.
# Like this.
print(msg['gender'] ,msg['first_name'], msg['last_name'])