Create a JSON File: Correct Format, Validation and Safe Formatting
How to write a valid JSON file: correct syntax, the difference between formatting and changing data, and what can break JSON when it is copied or shared.
A JSON file is a plain-text file that holds structured data in a strict, widely supported syntax. Creating one requires only a text editor, but the file is useful only if it is valid. This guide covers the correct format, what JSON formatters and validators actually change, and what can go wrong when JSON is copied, pasted or shared.
Quick verdict: when a formatter or validator fits your use
A JSON formatter or validator is worth using whenever the file will be read by a program, such as a configuration loader, an API or a build tool. The main benefit is that syntax errors are caught before they cause a failure somewhere harder to debug. Pretty-printing also makes nested data easier for people to review.
The decisive tradeoff is between formatting and modifying. Changing whitespace or indentation never changes the data. Some tools, however, offer options that do: sorting keys, converting numbers, removing duplicate keys or rewriting characters. Check which operations a tool performs before trusting its output as equivalent to the input.
JSON is the wrong choice when the content is meant only for people to read, such as notes, instructions or prose. Plain text or a document format is easier to write and read in that case. JSON also has no comment syntax, so configuration that needs inline explanations may suit a format designed for that purpose, if the consuming software supports one.
Correct JSON format: the rules that matter
The JSON specification (RFC 8259) defines a small grammar. A JSON file contains a single value, usually an object or an array. Most validation errors come from breaking one of the following rules:
- Objects are wrapped in curly braces and contain name/value pairs separated by commas.
- Arrays are wrapped in square brackets and contain values separated by commas.
- Names (keys) must be strings in double quotes.
- Strings must use double quotes. Single quotes are not valid.
- The allowed values are strings, numbers, objects, arrays, true, false and null. The three literals are lowercase.
- Trailing commas after the last item in an object or array are not allowed.
- Comments are not part of standard JSON.
- Whitespace between tokens (spaces, tabs, line breaks) is insignificant.
- Names within an object should be unique. Parsers handle duplicates inconsistently.
How to write a JSON object, step by step
- 1. Open a plain-text or code editor. A word processor can insert curly quotes and hidden formatting.
- 2. Write the outer structure, usually an opening and closing curly brace for an object.
- 3. Add each field as a double-quoted name, a colon and a value.
- 4. Separate fields with commas, with no comma after the last one.
- 5. Nest objects or arrays where the data has structure.
- 6. Save the file with a .json extension and UTF-8 encoding.
- 7. Run the file through a validator or a parser before using it.
A small, valid example of data in JSON format:
{
"name": "Example project",
"version": 2,
"active": true,
"tags": ["json", "config"],
"owner": null,
"settings": {
"retries": 3,
"timeoutSeconds": 30
}
}
The next example looks similar but is invalid for five reasons: the key is unquoted, the string uses single quotes, the literal is capitalized, a comment is present and there is a trailing comma.
{
name: 'Example project',
"active": True,
// comments are not allowed
"tags": ["json", "config",]
}
To check a file from the command line, Python's standard library includes a module that parses and pretty-prints JSON. It reports an error with a line and column number if the file is invalid:
python3 -m json.tool data.json
What formatting actually changes
In the context of JSON tools, "formatting" means rewriting the text without changing the data it represents. The distinction that matters is between the stored characters in the file and the data a parser produces from them. Pretty-printing and minifying change the characters but leave the data identical. Other operations can change the data itself.
| Operation | Stored characters change? | Parsed data changes? |
| Pretty-print (add indentation and line breaks) | Yes, whitespace only | No |
| Minify (remove insignificant whitespace) | Yes, whitespace only | No |
| Escape non-ASCII characters as \u sequences | Yes | No, the string value is the same |
| Sort keys alphabetically | Yes | Usually no for parsers that ignore order, but order-sensitive consumers may differ |
| Remove or merge duplicate keys | Yes | Yes, data may be lost |
| Reformat or round numbers | Yes | Possibly, depending on precision |
The same object, minified, is still valid JSON and parses to the same data as the pretty-printed version:
{"name":"Example project","version":2,"active":true,"tags":["json","config"],"owner":null,"settings":{"retries":3,"timeoutSeconds":30}}
Unicode escaping is another stored-character change that does not alter the value. The strings "caf\u00e9" and "café" parse to the same text. Escaping is useful when a destination cannot handle non-ASCII characters reliably. The unescaped form is easier for people to read.
Numbers need more care. The specification allows arbitrarily long number literals, but many parsers store numbers as double-precision floating-point values. Integers beyond about 9 quadrillion (2 to the 53rd power), and some decimal values, may not round-trip exactly. If a value such as an ID must be preserved exactly, storing it as a string is the safer choice. Whether a particular formatter rewrites numbers depends on the tool, so check its documentation.
Will the JSON stay valid after copying or sharing?
JSON is plain text, so it survives copying well, provided nothing alters the characters along the way. The common problems come from software that "helps" by changing the text:
- Smart quotes: word processors, some note apps and some chat tools replace straight double quotes with curly ones, which makes the JSON invalid.
- Invisible characters: non-breaking spaces, zero-width characters or a byte order mark at the start of a file can cause parse errors that are hard to see. The specification says implementations must not add a byte order mark, though parsers may choose to ignore one.
- Line wrapping and truncation: long minified lines can be cut off or wrapped by editors, email clients or messaging apps.
- Encoding changes: saving in an encoding other than UTF-8 can corrupt non-ASCII characters. UTF-8 is the encoding the specification requires for JSON exchanged between systems.
Behavior varies by destination and version. A paste that works in one editor or chat app is no guarantee for another. No tests of specific destinations were available for this guide. Validate the JSON after it arrives rather than assuming it survived intact. Sharing a file attachment, or using a code block where the destination supports one, generally reduces the risk of automatic character substitution.
For readability and accessibility, pretty-printed JSON with consistent indentation is easier to scan and review than a single minified line. No documentation was available here on how specific screen readers handle JSON, so that remains an open question. When JSON is shared with people rather than programs, a plain-language summary of what the data contains is a sensible companion to the raw file.
Online formatters and validators are convenient, but their appearance or marketing says nothing reliable about what happens to pasted data. Unless a tool documents its handling clearly, avoid pasting JSON that contains credentials, tokens or personal data. A local editor or command-line tool keeps the data on your machine.
Summary
To create a JSON file, write it in a plain-text editor with double-quoted keys and strings, no trailing commas and no comments. Save it as UTF-8 and validate it before use. Formatting changes only whitespace and is safe. Sorting keys, deduplicating and reformatting numbers can change the data, so use those deliberately. After copying or sharing JSON, validate it again where it ends up.