AjapaReporter

Home life · Updated

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

Time it takes10 min
Steps7in the list below
LevelEasyno special skills
Official sourceJSON.orgcheck it for changes

Quick answer Writing a JSON file means structuring data with key-value pairs and arrays, then saving it with a .json extension. You can do it in any text editor, but validation tools help catch errors.

What You’ll Need

  • A text editor (Notepad, TextEdit, VS Code, or any code editor)
  • Basic understanding of JavaScript object syntax (keys and values)
  • Optional: an online JSON validator or formatter

Step-by-Step

  1. Open a text editor. Use any plain text editor like Notepad (Windows) or TextEdit (Mac). For better syntax highlighting, use a code editor like VS Code, Sublime Text, or Atom.
  2. Start with curly braces. JSON objects are enclosed in curly braces {}. This defines the root of your data. For example: {} is valid empty JSON.
  3. Add key-value pairs. Inside the braces, write a key (a string in double quotes), followed by a colon, then a value. Example: "name": "John". Separate multiple pairs with commas.
  4. Use valid JSON data types. Values can be strings (in double quotes), numbers (no quotes), booleans (true/false), null, arrays (in square brackets), or other objects (nested curly braces). Example: "age": 30, "isStudent": false, "hobbies": ["reading", "coding"].
  5. Nest objects and arrays. For complex data, place objects inside objects or arrays. Ensure commas separate items correctly. Example: "address": {"city": "New York", "zip": "10001"}.
  6. Check for trailing commas. JSON does not allow trailing commas after the last item in an object or array. Remove any extra commas before closing braces or brackets.
  7. Save the file with .json extension. In your editor, choose “Save As” and name the file with a .json extension, like data.json. Ensure the file type is set to “All Files” or “JSON” to avoid adding .txt automatically.

Common Mistakes

  • Using single quotes. JSON requires double quotes for keys and string values. Single quotes are not allowed.
  • Forgetting commas. Each key-value pair (except the last) must be followed by a comma. Missing commas cause parse errors.
  • Trailing commas. Adding a comma after the last item is invalid in JSON. This is a common error when editing manually.
  • Unquoted keys. Keys must be enclosed in double quotes. Unquoted keys like name: "John" are invalid.
  • Comments. JSON does not support comments. Remove any // or /* */ comments before saving.

Tips

  • Use a validator. After writing, paste your JSON into an online validator like JSONLint to check for errors.
  • Format for readability. Indent nested objects and arrays with spaces or tabs. This makes it easier to spot errors.
  • Use a code editor. Editors like VS Code highlight syntax and can flag missing commas or quotes as you type.
  • Start small. Test with a simple object, then add complexity. This isolates errors.
  • Remember UTF-8 encoding. Save files as UTF-8 to support all characters, especially if using non-English text.

Troubleshooting

  • File not recognized as JSON. Ensure the file extension is .json, not .txt or .json.txt. In Windows, you may need to enable “Show file extensions” in File Explorer.
  • Parse errors. Use an online validator to locate the exact line and column of the error. Common fixes: add missing commas, remove trailing commas, or double-check quotes.
  • Unexpected token. This usually means a stray character like a semicolon or a single quote. Review the highlighted area in the validator.
  • Large files. For big files, use a JSON formatter tool to beautify and identify structural issues. Break the file into smaller parts if needed.
  • Encoding issues. If special characters appear garbled, re-save the file with UTF-8 encoding (most editors have this option in Save As).

Frequently Asked Questions

Can I use comments in a JSON file?

No, JSON does not support comments. If you need to document data, consider adding a “_comment” key or use a separate documentation file.

How do I write an empty array or object?

An empty array is [] and an empty object is {}. Both are valid JSON values.

What is the difference between JSON and JavaScript objects?

JSON is a text format with strict rules: keys must be double-quoted, no functions or undefined values, and no trailing commas. JavaScript objects are code and can have unquoted keys, functions, and comments.

Can I use a JSON file for configuration?

Yes, many applications use JSON for config files (e.g., package.json for Node.js). Just ensure the structure matches what the application expects.

How do I validate JSON on Windows without installing anything?

Use a web browser: open a new tab, press F12 to open developer tools, go to the Console tab, and type JSON.parse('your JSON here'). If no error appears, it’s valid.

What if I need to write JSON with a lot of nested data?

Break it down into smaller objects and arrays, and use a formatter to keep it readable. Consider using a schema validator to ensure data integrity.

Sources: JSON.org. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.