Posts: 16
Threads: 6
Joined: Nov 2019
Oct-30-2021, 11:06 AM
(This post was last modified: Oct-30-2021, 11:06 AM by JgKSuperstar.)
Hello, how do I solve this problem?
import json
def write_json(new_date, filename='data.json'):
try:
with open(filename, 'r+') as file:
#First we load existing data into a dict. (Önce mevcut verileri bir dict'e yüklüyoruz)
file_data = json.load(file)
file_data["emp_details"].append(new_date)
# Sets file's current position at offset. (Dosyanın geçerli konumunu ofset olarak ayarlar.)
file.seek(0)
# convert back to json. (Json'a dönüştür.)
json.dump(file_data, file, indent=4)
# python object to be appended (Eklenecek python nesnesi)
except json.decoder.JSONDecodeError:
return
y = {"emp_name":"Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
}
write_json(y)
Error: raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Posts: 8,129
Threads: 159
Joined: Sep 2016
Oct-30-2021, 11:21 AM
(This post was last modified: Oct-30-2021, 11:22 AM by buran.)
Your current snippet raise different error
Error: Traceback (most recent call last):
File "***", line **, in <module>
write_json(y)
File "***", line **, in write_json
file_data["emp_details"].append(new_date)
TypeError: list indices must be integers or slices, not str
JgKSuperstar likes this post
Posts: 7,286
Threads: 122
Joined: Sep 2016
file_data = json.load(file) When you do this file_data it's a dictionary.
Then on line-11 can not use .append as that method is only for list.
For dictionary can use .update
Don't use try: except with return nothing,when test out stuff want to see all errors.
Quick test working.
import json
from pprint import pprint
def write_json(new_date, filename='data.json'):
with open(filename, 'r+') as file:
file_data = json.load(file)
#print(file_data)
file_data["scripts"].update(new_date)
#pprint(file_data)
file.seek(0)
json.dump(file_data, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y)
JgKSuperstar likes this post
Posts: 16
Threads: 6
Joined: Nov 2019
(Oct-30-2021, 11:21 AM)buran Wrote: Your current snippet raise different error
Error: Traceback (most recent call last):
File "***", line **, in <module>
write_json(y)
File "***", line **, in write_json
file_data["emp_details"].append(new_date)
TypeError: list indices must be integers or slices, not str
I'm sorry, the code I gave you is wrong. No blocks try and except.
Posts: 16
Threads: 6
Joined: Nov 2019
(Oct-30-2021, 12:16 PM)snippsat Wrote: file_data = json.load(file) When you do this file_data it's a dictionary.
Then on line-11 can not use .append as that method is only for list.
For dictionary can use .update
Don't use try: except with return nothing,when test out stuff want to see all errors.
Quick test working.
import json
from pprint import pprint
def write_json(new_date, filename='data.json'):
with open(filename, 'r+') as file:
file_data = json.load(file)
#print(file_data)
file_data["scripts"].update(new_date)
#pprint(file_data)
file.seek(0)
json.dump(file_data, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y)
I'm getting the same mistake again. Could it be that when we turn it on to read mode, the "data.json" file is empty and gives it this error?
![]() ![[Image: jdec33t.png]](https://i.hizliresim.com/jdec33t.png) ';" src=" ![[Image: jdec33t.png]](https://i.hizliresim.com/jdec33t.png) " alt="İmage" id="maximage" title="Click Photo To Enlarge">
Posts: 7,286
Threads: 122
Joined: Sep 2016
Oct-30-2021, 04:33 PM
(This post was last modified: Oct-30-2021, 04:33 PM by snippsat.)
You don't show content of data.json .
Here is my test with a random validated json file.
data.json before:
{
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"prettier": "^2.0.2"
},
"scripts": {
"eslint": "eslint . --fix",
"eslint:check": "eslint .",
"lint": "npm run prettier && npm run eslint"
},
"dependencies": {
"@jsxtools/resize-observer": "^1.0.4"
}
} import json
from pprint import pprint
def write_json(new_date, filename='data.json'):
with open(filename, 'r+') as file:
file_data = json.load(file)
#print(file_data)
file_data["scripts"].update(new_date)
#pprint(file_data)
file.seek(0)
json.dump(file_data, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y) data.json after:
Output: {
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"prettier": "^2.0.2"
},
"scripts": {
"eslint": "eslint . --fix",
"eslint:check": "eslint .",
"lint": "npm run prettier && npm run eslint",
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
},
"dependencies": {
"@jsxtools/resize-observer": "^1.0.4"
}
}
Posts: 16
Threads: 6
Joined: Nov 2019
(Oct-30-2021, 04:33 PM)snippsat Wrote: You don't show content of data.json .
Here is my test with a random validated json file.
data.json before:
{
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"prettier": "^2.0.2"
},
"scripts": {
"eslint": "eslint . --fix",
"eslint:check": "eslint .",
"lint": "npm run prettier && npm run eslint"
},
"dependencies": {
"@jsxtools/resize-observer": "^1.0.4"
}
} import json
from pprint import pprint
def write_json(new_date, filename='data.json'):
with open(filename, 'r+') as file:
file_data = json.load(file)
#print(file_data)
file_data["scripts"].update(new_date)
#pprint(file_data)
file.seek(0)
json.dump(file_data, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y) data.json after:
Output: {
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"prettier": "^2.0.2"
},
"scripts": {
"eslint": "eslint . --fix",
"eslint:check": "eslint .",
"lint": "npm run prettier && npm run eslint",
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
},
"dependencies": {
"@jsxtools/resize-observer": "^1.0.4"
}
}
My json file is empty
Posts: 16
Threads: 6
Joined: Nov 2019
If the json file is not empty, the yes code does not give an error
Posts: 7,286
Threads: 122
Joined: Sep 2016
Oct-30-2021, 10:18 PM
(This post was last modified: Oct-30-2021, 10:19 PM by snippsat.)
(Oct-30-2021, 10:12 PM)squad Wrote: If the json file is not empty, the yes code does not give an error If start with empty file can not use json.load(file) on that file.
Make the the structure you want first with a dictionary,then json.dump to make it json file.
Example.
import json
from pprint import pprint
def write_json(new_date, filename='d.json'):
json_make = {}
with open(filename, 'r+') as file:
json_make["emp_details"] = new_date
file.seek(0)
json.dump(json_make, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y) Output: {
"emp_details": {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
}
}
JgKSuperstar likes this post
Posts: 16
Threads: 6
Joined: Nov 2019
(Oct-30-2021, 10:18 PM)snippsat Wrote: (Oct-30-2021, 10:12 PM)squad Wrote: If the json file is not empty, the yes code does not give an error If start with empty file can not use json.load(file) on that file.
Make the the structure you want first with a dictionary,then json.dump to make it json file.
Example.
import json
from pprint import pprint
def write_json(new_date, filename='d.json'):
json_make = {}
with open(filename, 'r+') as file:
json_make["emp_details"] = new_date
file.seek(0)
json.dump(json_make, file, indent=4)
if __name__ == '__main__':
y = {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time",
}
write_json(y) Output: {
"emp_details": {
"emp_name": "Nikhil",
"email": "[email protected]",
"job_profile": "Full Time"
}
}
I'm sorry for troubling you. Thank you very much for your patience and help my problem has been solved :) <3
|