How to Create a JSON File: Step-by-Step Guide

Complete guide to creating JSON files manually, programmatically, and using tools

Published: January 2025 • 8 min read

Creating a JSON file is straightforward - you can use a simple text editor, write code to generate it programmatically, or use online tools. JSON files are plain text, making them easy to create and edit without specialized software.

This guide covers multiple methods to create JSON files, from manual creation for beginners to automated generation for developers. New to JSON? Read what is JSON first, or learn about JSON file structure.

Method 1: Create JSON File Manually

Step 1: Open a Text Editor

  • Windows: Notepad, Notepad++, or Visual Studio Code
  • Mac: TextEdit, Sublime Text, or Visual Studio Code
  • Linux: gedit, nano, vim, or Visual Studio Code

Step 2: Write JSON Content

Start with a basic JSON object. Here's a simple example:

{
  "server": {
    "hostname": "api.network.com",
    "port": 443,
    "protocol": "https"
  },
  "database": {
    "host": "db.network.com",
    "name": "production",
    "maxConnections": 100
  },
  "features": {
    "caching": true,
    "logging": true
  }
}

Step 3: Save the File

  1. Click "File" → "Save As"
  2. Choose a location
  3. Name your file with .json extension (e.g., config.json)
  4. Select "All Files" as file type (if using Notepad)
  5. Click "Save"

Important: Make sure to use the .json extension. This helps operating systems and applications recognize the file as JSON.

Method 2: Use Online JSON Tools

Online tools make it easy to create and validate JSON files with built-in formatting and error checking.

Using Our Tools:

  1. Visit JSON Formatter
  2. Write or paste your JSON content
  3. Click "Format" to beautify and validate
  4. Copy the formatted JSON
  5. Save to a .json file on your computer

Online tools automatically validate syntax, highlight errors, and format your JSON properly.

Method 3: Generate JSON Programmatically

For dynamic data or automation, create JSON files using code:

Python

import json

# Create data structure
data = {
    "server": {
        "hostname": "api.network.com",
        "port": 443
    },
    "features": {
        "caching": True,
        "logging": True
    }
}

# Write to JSON file
with open('config.json', 'w') as file:
    json.dump(data, file, indent=2)

print("JSON file created successfully!")

JavaScript (Node.js)

const fs = require('fs');

// Create data structure
const data = {
  server: {
    hostname: 'api.network.com',
    port: 443
  },
  features: {
    caching: true,
    logging: true
  }
};

// Write to JSON file
fs.writeFileSync(
  'config.json',
  JSON.stringify(data, null, 2)
);

console.log('JSON file created successfully!');

Java

import org.json.JSONObject;
import java.nio.file.Files;
import java.nio.file.Paths;

// Create JSON object
JSONObject data = new JSONObject();
JSONObject server = new JSONObject();
server.put("hostname", "api.network.com");
server.put("port", 443);
data.put("server", server);

// Write to file
Files.write(
  Paths.get("config.json"),
  data.toString(2).getBytes()
);

C#

using System.IO;
using System.Text.Json;

// Create data structure
var data = new {
    server = new {
        hostname = "api.network.com",
        port = 443
    },
    features = new {
        caching = true,
        logging = true
    }
};

// Write to JSON file
string jsonString = JsonSerializer.Serialize(
    data, 
    new JsonSerializerOptions { WriteIndented = true }
);
File.WriteAllText("config.json", jsonString);

JSON Syntax Rules to Remember

Follow these rules to create valid JSON files:

1. Use Double Quotes

Property names and string values must use double quotes, not single quotes.

2. No Trailing Commas

Remove commas after the last item in objects and arrays.

3. Proper Data Types

Use correct types: strings in quotes, numbers without quotes, true/false for booleans.

4. Balanced Brackets

Every opening brace { must have a closing brace }.

5. No Comments

Standard JSON does not support comments. Use separate documentation.

Best Practices

  • 1.Use proper indentation - 2 or 4 spaces for readability
  • 2.Validate before saving - Use JSON Validator to check syntax
  • 3.Use consistent naming - Choose camelCase or snake_case and stick with it
  • 4.Keep it simple - Avoid excessive nesting when possible
  • 5.Add descriptive names - Use clear property names that describe the data

JSON Tools

Continue Learning

External References

  • JSON.org - Official JSON specification
  • RFC 8259 - JSON data interchange format