DeveloperBreeze


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}'.")

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
php

Securing Laravel Applications Against Common Vulnerabilities

   $users = DB::table('users')->where('email', $email)->get();

Or use bindings in raw queries:

Nov 16, 2024
Read More
Tutorial
javascript

التعامل مع JSON في JavaScript: قراءة البيانات وكتابتها

في هذا الدليل، سنتعلم كيفية التعامل مع JSON في JavaScript، بما في ذلك قراءة البيانات وكتابتها.

JSON هو تنسيق يستخدم في تبادل البيانات يشبه الكائنات في JavaScript. يتكون JSON من أزواج مفتاح-قيمة (key-value pairs) تمامًا مثل الكائنات، وهو مدعوم في معظم لغات البرمجة.

Sep 26, 2024
Read More
Tutorial
javascript

AJAX with JavaScript: A Practical Guide

In many real-world applications, you'll need to send data to the server using POST requests. Here's how to send form data using the Fetch API.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>POST Request with Fetch</title>
</head>
<body>
    <form id="postDataForm">
        <input type="text" id="title" placeholder="Enter title" required>
        <input type="text" id="body" placeholder="Enter body" required>
        <button type="submit">Submit</button>
    </form>
    <div id="postDataOutput"></div>

    <script>
        document.getElementById('postDataForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const title = document.getElementById('title').value;
            const body = document.getElementById('body').value;

            fetch('https://jsonplaceholder.typicode.com/posts', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    title: title,
                    body: body
                })
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('postDataOutput').innerHTML = `
                    <h3>Data Submitted:</h3>
                    <p>Title: ${data.title}</p>
                    <p>Body: ${data.body}</p>
                `;
            })
            .catch(error => console.error('Error:', error));
        });
    </script>
</body>
</html>

Sep 18, 2024
Read More
Tutorial
go

Building a RESTful API with Go and Gorilla Mux

   go mod init booksapi
   go get -u github.com/gorilla/mux

Aug 12, 2024
Read More
Code
javascript json

How to Deep Clone a JavaScript Object

No preview available for this content.

Aug 12, 2024
Read More
Code
json python

Python Code Snippet: Simple RESTful API with FastAPI

No preview available for this content.

Aug 04, 2024
Read More
Code
javascript json

JavaScript Code Snippet: Fetch and Display Data from an API

No preview available for this content.

Aug 04, 2024
Read More
Tutorial
python

Creating a Simple REST API with Flask

Run the application:

python app.py

Aug 03, 2024
Read More
Code
javascript

Fetch JSON Data from API in JavaScript

No preview available for this content.

Jan 26, 2024
Read More
Code
python

JSON Serialization and Deserialization

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Read JSON Data from a File

No preview available for this content.

Jan 26, 2024
Read More
Code
php

JSON File Reading and Decoding

$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
print_r($data);

Jan 26, 2024
Read More
Code
bash

Various cURL Examples for API Interactions

No preview available for this content.

Jan 26, 2024
Read More
Code
python

File Reading and Writing

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate Fibonacci Sequence

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Sort a List

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Calculate Sum of Numbers in a List Using sum() Function

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate List of Even Numbers Using List Comprehension

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Find Maximum Number in List Using max() Function

numbers = [12, 34, 23, 56, 45, 67]
max_number = max(numbers)
print('Maximum Number:', max_number)

Jan 26, 2024
Read More
Code
javascript

Find Maximum Number in Array Using Math.max

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!