What JSON is and how to use JSON files
Learn what JSON is, what the acronym means, whether it counts as a file type, language or code, and how to open, validate, reformat and load JSON files safely.
What JSON is
JSON is a plain-text format for storing and exchanging structured data. The acronym stands for JavaScript Object Notation. A JSON file holds data such as settings, API responses or lists of records, written as named values and ordered lists that both people and programs can read. The file does nothing by itself. A program reads it, turns the text into data structures and decides what to do with them.
The building blocks of JSON
Every JSON document is built from six kinds of values:
- Object: a set of name/value pairs inside curly braces, such as {"name": "Ada"}
- Array: an ordered list of values inside square brackets, such as [1, 2, 3]
- String: text inside double quotes, such as "hello"
- Number: an integer or decimal, such as 42 or -3.5
- Boolean: the bare words true or false
- Null: the bare word null, meaning no value
Example (illustrative) of a small JSON document:
{
"name": "Ada",
"active": true,
"roles": ["admin", "editor"],
"manager": null,
"logins": 42
}
JSON's syntax is strict, and the rules below catch most people who are used to looser formats:
- Keys and strings must use double quotes. Single quotes are invalid.
- A comma may not follow the last item in an object or array.
- Comments are not allowed.
- Values such as undefined, NaN and Infinity are not part of JSON.
- Inside a string, a line break, tab or double quote must be escaped as \n, \t or ".
- Spaces, tabs and line breaks between values carry no meaning, so a file can be compact or indented and still hold the same data.
Is JSON a file type, a language or code?
File type: Yes. JSON files normally use the .json extension, and the registered media type is application/json. Because the content is plain text, any text editor can open it. The IETF specification requires UTF-8 encoding for JSON exchanged between systems, so UTF-8 is the safe choice when you save a JSON file.
Programming language: No. JSON has no variables, conditions, loops or functions, so it cannot compute anything. It is a data format with a formal grammar. Calling it a notation or a data language fits better than calling it a programming language.
Markup language: No. A markup language such as HTML or XML wraps document text in tags to describe structure or presentation. JSON has no tags and no free-flowing document text. It describes data structures directly. People often compare JSON with XML because both are used for data exchange, but they work differently.
Is it code? JSON is written in a precise syntax, often sits next to source code and is edited in code editors. It is still data, not executable instructions. Its syntax resembles JavaScript, but JSON does not depend on any one language. Parsers are available for most mainstream languages.
Why it is called JavaScript Object Notation
The name comes from the syntax. JSON objects and arrays are written the same way JavaScript writes object and array literals. JSON has since been standardized on its own terms, and the current definitions are published as ECMA-404 and IETF RFC 8259. The sources available for this guide do not include confirmed dates or attribution for JSON's creation, so those details are left out.
How to open or "run" a JSON file
You cannot run a JSON file the way you run a script, because it contains no instructions. Double-clicking it opens whatever application your system associates with .json files. When people talk about running a JSON file, they usually mean one of three tasks: viewing it, checking that it is valid, or having a program load it.
- 1. Make a copy of the file before you edit or reformat it.
- 2. Open it in a text or code editor. Many code editors highlight JSON syntax and flag errors as you type. Some web browsers also include a built-in JSON viewer.
- 3. Validate it with a command-line tool, as described in the next section.
- 4. Load it from a program when you need to work with the data.
Check that a JSON file is valid
Prerequisite: Python 3, which includes a JSON checker in its standard library. In a terminal, change to the folder that contains the file and run:
python3 -m json.tool data.json
If the file is valid, the command prints the data in an indented layout. If the file is invalid, it prints an error message that names a line and column. Look at that position and the text just before it. If jq is installed (it is a separate download), this command does the same check:
jq . data.json
These problems cause most validation errors:
- A trailing comma after the last item, as in [1, 2, 3,]
- Single-quoted strings or keys, such as {'name': 'Ada'}
- Keys without quotes, such as {name: "Ada"}
- Comments starting with // or /*
- A missing comma between two items
- An invisible byte order mark (BOM) at the start of the file, which some editors add when saving as UTF-8. Some parsers reject it, and Python's json module raises an error about an unexpected UTF-8 BOM.
- Several JSON values in one file, one per line. This layout is called JSON Lines (often saved as .jsonl). It is a valid format of its own, but the file as a whole is not a single JSON document, so whole-file validators reject it.
Reformat a JSON file without losing content
Pretty-printing adds indentation and line breaks so the file is easier to read. Before (illustrative input in data.json):
{"name":"Ada","tags":["a","b"],"city":"Zürich"}
Write the formatted result to a new file:
python3 -m json.tool --no-ensure-ascii data.json > data.pretty.json
After (illustrative expected output with Python's default four-space indent; not a verified run):
{
"name": "Ada",
"tags": [
"a",
"b"
],
"city": "Zürich"
}
The --no-ensure-ascii option exists only in recent Python versions. Without it, json.tool rewrites non-ASCII characters as escape sequences, so "Zürich" becomes "Z\u00fcrich". Parsers treat the two forms as the same data, but the visible text changes. If your Python version rejects the option, either accept the escapes or use jq, which writes UTF-8 characters as they are.
Never redirect the output back into the file you are reading, as in python3 -m json.tool data.json > data.json. The shell empties data.json before Python reads it, and you lose the original content. Write to a new file, check it, and replace the original only after the check passes.
To confirm the reformatted file holds the same data as the original, compare the parsed contents:
python3 -c "import json; a = json.load(open('data.json', encoding='utf-8')); b = json.load(open('data.pretty.json', encoding='utf-8')); print(a == b)"
True means the data matches. This comparison ignores key order and whitespace and checks only the values. Watch for these changes that formatting tools can introduce:
- Key sorting: options such as --sort-keys reorder keys. The JSON standard treats object keys as unordered, but some people and tools still rely on the original order, and reordering creates large diffs.
- Duplicate keys: the standard says keys should be unique, but parsers handle duplicates differently. Python keeps the last value silently, so reformatting can drop the earlier ones without warning.
- Numbers: some tools convert numbers to floating-point values, which can change very large integers or values with many decimal places. Check IDs and precise numbers after reformatting.
To go the other direction and produce compact, single-line output, jq offers the -c option:
jq -c . data.json > data.min.json
Use JSON data in a program
Example (illustrative) in Python, reading the file and printing one value:
import json
with open("data.json", encoding="utf-8") as f:
data = json.load(f)
print(data["name"])
If the file starts with a byte order mark, open it with encoding="utf-8-sig" instead. To save changes, use json.dump(data, f, indent=2, ensure_ascii=False). Write to a new file first, for the same reason given for reformatting.
Example (illustrative) in JavaScript with Node.js:
const fs = require("fs");
const data = JSON.parse(fs.readFileSync("data.json", "utf8"));
console.log(data.name);
In both languages, invalid JSON raises an error instead of returning partial data. If a load fails, run the validation command above to find the exact line and column.