import json
# Define the JSON file to read and write
json_file = 'list.json'
# Read the existing JSON data
try:
with open(json_file, 'r') as file:
json_list = json.load(file)
print("Successfully loaded the JSON file.")
except FileNotFoundError:
print(f"File '{json_file}' not found. Initializing with an empty list.")
json_list = []
except json.JSONDecodeError:
print(f"File '{json_file}' is not a valid JSON file. Initializing with an empty list.")
json_list = []
# New data to add to the JSON
new_entry = {'Name': 'The Good Coder', 'Hobbies': 'Code, Write Code!'}
# Append the new data to the JSON list
json_list.append(new_entry)
print("New entry added to the JSON list.")
# Write the updated list back to the JSON file
with open(json_file, 'w') as file:
json.dump(json_list, file, indent=4)
print(f"Updated JSON data written to '{json_file}'.")
Append Data to JSON File
Related Posts
More content you might like
Tutorial
php
Securing Laravel Applications Against Common Vulnerabilities
$user = User::where('email', $email)->first();Use the {{ }} syntax in Blade templates to automatically escape user input:
Nov 16, 2024
Read More Tutorial
javascript
التعامل مع JSON في JavaScript: قراءة البيانات وكتابتها
{
"name": "أحمد",
"address": {
"city": "الرياض",
"postalCode": "12345"
},
"skills": ["برمجة", "تصميم", "تحليل"]
}const data = {
name: "أحمد",
address: {
city: "الرياض",
postalCode: "12345"
},
skills: ["برمجة", "تصميم", "تحليل"]
};
console.log(data.address.city); // الرياض
console.log(data.skills[0]); // برمجةSep 26, 2024
Read More Tutorial
javascript
AJAX with JavaScript: A Practical Guide
With the Fetch API:
- The
fetch()method returns a Promise that resolves to the Response object representing the entire HTTP response. - We check if the response is successful and then parse it as JSON.
- Any errors during the request are caught and logged.
Sep 18, 2024
Read More Tutorial
go
Building a RESTful API with Go and Gorilla Mux
mkdir booksapi
cd booksapi go mod init booksapiAug 12, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!