How to Create a JSON File: A Complete Practical Guide
JSON (JavaScript Object Notation) has become the universal language of data exchange. Whether you're configuring an app, storing structured data, or communicating between services, knowing how to create a JSON file correctly is a foundational skill. The good news: it's simpler than it looks.
What Is a JSON File?
A JSON file is a plain text file that stores data in a structured, human-readable format using key-value pairs. It uses the .json file extension and follows a strict syntax derived from JavaScript — though it works with virtually every modern programming language.
JSON is used everywhere: REST APIs, configuration files, databases like MongoDB, package managers like npm, and app settings across Android, iOS, and desktop platforms.
A basic JSON file looks like this:
{ "name": "Alex", "age": 30, "isActive": true, "skills": ["Python", "JavaScript", "SQL"] } The structure is clean and logical — even non-developers can read it at a glance.
Core JSON Syntax Rules You Must Follow
JSON is unforgiving about syntax. One misplaced comma or missing bracket will break the entire file. Before creating one, understand these rules:
- Keys must be strings wrapped in double quotes — not single quotes
- Values can be strings, numbers, booleans (
true/false), arrays, objects, ornull - No trailing commas after the last item in an object or array
- No comments — unlike JavaScript or Python, JSON does not support
//or/* */ - The entire file must be wrapped in either a
{}object or a[]array at the top level
| Value Type | Example |
|---|---|
| String | "city": "London" |
| Number | "score": 98 |
| Boolean | "verified": true |
| Array | "tags": ["tech", "api"] |
| Object | "address": { "zip": "10001" } |
| Null | "middleName": null |
Method 1: Create a JSON File With a Text Editor
The most straightforward approach — no special tools required.
On Windows:
- Open Notepad (or any plain text editor)
- Write your JSON content
- Go to File → Save As
- Set the file name to something like
data.json— include the.jsonextension explicitly - Change Save as type to All Files to prevent Notepad from adding
.txt
On macOS:
- Open TextEdit
- Go to Format → Make Plain Text — critical step, or the file won't save cleanly
- Write your JSON and save with a
.jsonextension
On Linux: Any terminal text editor works — nano, vim, or gedit. Create a new file directly:
nano data.json Write your content, save, and exit.
Method 2: Create a JSON File Using a Code Editor 🛠️
For anyone working with JSON regularly, a dedicated code editor is the better choice. Editors like VS Code, Sublime Text, or Notepad++ offer:
- Syntax highlighting — color-coded keys, values, and structure
- Auto-formatting — instantly readable indentation
- Error detection — flags invalid syntax before you save
- JSON schema validation — confirms your data matches an expected structure
In VS Code specifically:
- Open a new file
- Press
Ctrl+Shift+P(orCmd+Shift+Pon macOS) and type Change Language Mode - Select JSON
- Write your data — the editor will flag any syntax errors in real time
- Save as
filename.json
This workflow catches mistakes before they cause problems downstream.
Method 3: Generate a JSON File Programmatically
If your data comes from another source — a database, user input, or an API — you'll typically generate the JSON file using code rather than writing it by hand.
Python example:
import json data = { "product": "Laptop", "price": 899, "inStock": True } with open("output.json", "w") as f: json.dump(data, f, indent=2) JavaScript (Node.js) example:
const fs = require("fs"); const data = { product: "Laptop", price: 899, inStock: true }; fs.writeFileSync("output.json", JSON.stringify(data, null, 2)); The indent=2 or null, 2 argument in both examples adds human-readable formatting. Without it, the output is valid JSON but written on a single line — harder to read, but functionally identical.
Method 4: Use an Online JSON Tool
For quick testing or one-off files, online tools like JSONLint, JSON Editor Online, or Postman let you write, validate, and export JSON without installing anything.
These tools are particularly useful for:
- Validating existing JSON files for syntax errors
- Formatting minified JSON into readable output
- Prototyping data structures before building them into an application
Validating Your JSON File 🔍
Creating the file is only half the job. Malformed JSON causes silent failures in apps and APIs. Always validate before deploying.
Quick validation options:
- JSONLint.com — paste your JSON and it highlights exact errors
- VS Code built-in — hover over red underlines for error details
- Command line (Python):
python -m json.tool data.json— prints formatted output or reports the error line
What Affects How You Should Create Your JSON File
The "best" method isn't universal — it depends on several factors specific to your situation:
- Your technical skill level — command-line and programmatic approaches are more powerful but require comfort with code
- The size of the data — small config files are easy to write by hand; large datasets should be generated programmatically
- Your workflow — if you're building an API, generating JSON in code is standard; if you're writing a config file, a text editor may be all you need
- Validation requirements — production environments often require schema validation (using tools like JSON Schema), not just syntax checking
- The target system — some platforms (like certain APIs or databases) expect specific JSON structures or encoding standards like UTF-8
A developer building a data pipeline has different needs than someone editing a config file for a desktop app. The method that's overkill for one is essential for the other.