Database.json May 2026

You can now send a POST request to http://localhost:3000/posts using fetch in JavaScript: javascript

fetch('http://localhost:3000/posts', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ title: "My First Post", content: "This is some data saved to database.json!" }), }) .then(response => response.json()) .then(data => console.log('Success:', data)); Use code with caution. Copied to clipboard 2. Using Node.js (Direct File Writing)

Create a database.json with an initial structure: { "posts": [] } Use code with caution. Copied to clipboard database.json

import json new_post = {"id": 1, "title": "Hello from Python", "content": "Writing to JSON is easy!"} # Load existing data with open('database.json', 'r+') as file: db = json.load(file) db['posts'].append(new_post) # Seek to start and overwrite file.seek(0) json.dump(db, file, indent=2) Use code with caution. Copied to clipboard Best Practices for database.json

To write a new entry (a "post") to a database.json file, you can either use a tool like for a quick mock API or write a script in a language like JavaScript (Node.js) or Python to handle the file operations directly. 1. Using json-server (Quickest for Web Devs) You can now send a POST request to

If you want to treat database.json like a real REST API, json-server is the standard choice.

: JSON requires double quotes for keys and string values; single quotes will cause an error. Copied to clipboard import json new_post = {"id":

In your terminal, run: npx json-server --watch database.json