On this page

JSON is a text format for representing structured data so that applications can exchange and store information. Its name stands for JavaScript Object Notation. It gives data a predictable arrangement: labels identify values, and values can be grouped into records or lists. Although its notation comes from JavaScript, programs written in many languages can read and generate JSON. You can also open that text yourself to inspect a product record, an application’s settings, or an exported file. The format’s shared rules are defined in the ECMA-404 standard.

A simple JSON example

This complete, valid JSON document describes a fictional product:

{
  "product": "Notebook",
  "price": 49.9,
  "available": true
}

Read it as a small product card: its name is Notebook, its price is 49.9, and it is available. The example does not specify a currency.

Each label is a key, and the information next to it is its value. In "product": "Notebook", the key is "product" and its value is "Notebook". Together, they form a key–value pair, also called a member or property.

The surrounding braces, { and }, group those pairs into an object. A colon separates each key from its value, and a comma separates one pair from the next. The last pair has no comma after it.

Notice that the name has quotation marks, while the price and true do not. Those differences describe the kinds of values being represented. The label "price" alone does not make its value a number; writing 49.9 without quotation marks does.

What is JSON used for?

JSON is useful when software needs to represent information in a form another program can interpret. Consider three possible uses:

  • Exchanging product information. A shop’s server prepares our product record as JSON. A shopping app receives it, reads the name and price, and displays them on screen. The programs might communicate through an API, an interface through which programs exchange requests and information. JSON supplies the representation of the data; the API defines how the exchange works.
  • Reading configuration. A developer writes settings in a JSON file. When an application starts, it reads the file and applies the settings it recognizes, such as a chosen language or a display preference.
  • Exporting and saving records. A catalog application generates JSON for selected products and writes it to a file. Another application can later read that file and import the records if it supports their structure.

In each case, the programs must agree on what the fields mean. JSON defines how to write a key and value; it does not tell the receiving app what to do with a price or which currency to use.

Objects, arrays, and the six JSON data types

We can add more information to the same product:

{
  "product": "Notebook",
  "price": 49.9,
  "available": true,
  "colors": ["blue", "black"],
  "seller": {
    "name": "Ana",
    "city": "Puebla"
  },
  "notes": null
}

The square brackets around the colors create an array, an ordered list of values. Here, blue comes first and black second. The braces after "seller" contain another object, with its own name and city fields. Placing a structure inside another structure is called nesting.

To locate a value inside objects and arrays, follow how to explore nested JSON and copy a value’s path. The example distinguishes array indexes from literal property names containing dots or brackets.

JSON has six data types. Every one appears in this expanded example. JSON.org’s format reference describes their notation.

Type Value from the example How to read it
String "Notebook" Text enclosed in double quotation marks.
Number 49.9 A numeric value, written without quotation marks.
Boolean true A true-or-false value; here, the product is available.
Array ["blue", "black"] An ordered list containing two strings.
Object {"name":"Ana","city":"Puebla"} Related values grouped under their own keys.
Null null An explicit null value.

For this fictional catalog, we agree that "notes": null means no note has been recorded. That meaning belongs to this application’s convention. A null value differs from zero, an empty string, or leaving the notes property out entirely.

Quotation marks can change a value’s type

All six documents below are valid JSON. In each pair, the quotation marks change the type of the value:

Field Original type String instead
Price {"price":49.9} — number {"price":"49.9"} — text containing digits
Availability {"available":true} — boolean {"available":"true"} — text containing the word true
Notes {"notes":null} — null {"notes":"null"} — text containing the word null

This matters because an application may require a number for calculations or a boolean for a setting. A string that looks similar still has a different type.

A complete JSON document can also be an array or a single value, such as 42, "hello", or null; it does not have to begin with {. Arrays can contain different types of values. Dates have no dedicated JSON type, so applications use an agreed representation, often a string.

JSON text, a JSON file, and data inside a program

These three things are closely related:

Concept What it is Example
JSON text Information written according to JSON’s rules. The product record shown above.
A .json file A file containing that text. product.json, saved on your computer.
A value used by a program Data obtained when software interprets the text. A product object whose price the application can read or change.

JSON can travel between applications without ever becoming a file. Conversely, saving it in a file lets software read it later. Changing a filename to end in .json does not check or convert its contents: the text inside must still follow the format.

When a program interprets JSON text, the operation is called parsing. The resulting values belong to that programming language. Generating JSON text from compatible program values is called serialization.

JavaScript is one language that provides both operations. Its JSON.parse() method reads JSON text, while JSON.stringify() generates it. Generating text does not save a file; writing that text to storage is a separate step. The optional JavaScript example below shows this distinction in practice.

JSON syntax rules and five common mistakes

Syntax means the rules for writing the format. JSON requires straight double quotation marks around keys and strings, and commas between neighboring members or array elements. Matching braces and brackets close each structure.

Each entry in the “Invalid input” column below is deliberately incorrect. The corrected column contains complete, valid documents.

Mistake Invalid input Valid correction
Key has no quotation marks {product:"Notebook"} {"product":"Notebook"}
Single quotation marks {'product':'Notebook'} {"product":"Notebook"}
Comma after the final value {"price":49.9,} {"price":49.9}
Missing comma between members {"product":"Notebook" "price":49.9} {"product":"Notebook","price":49.9}
Capitalized boolean {"available":True} {"available":true}

Use true, false, and null exactly as written, in lowercase. Decimal numbers use a point, as in 49.9. Standard JSON does not allow comments, functions, or JavaScript values such as undefined, NaN, and Infinity. JavaScript permits some notation that JSON rejects, so a working JavaScript object example is not automatically valid JSON. MDN explains these differences.

Give each key a unique name within its object. Duplicate keys can produce different results in different programs, even when a parser accepts the text. Also avoid making an object’s meaning depend on the order of its properties; use an array when sequence matters. Software may impose limits on numeric size or precision, too. These are interoperability considerations described in RFC 8259.

If a program has rejected your document, follow the guide to finding and fixing JSON syntax errors for a worked example and minimal corrections.

How to open, edit, and validate a JSON file

Open or create the file

Open an existing .json file from a text or code editor’s file menu. To create one, start a new plain-text document and copy the first, three-field product example into it. You can read and edit it without installing a programming environment.

After editing, check the syntax with a JSON validator or an editor configured for standard JSON. Correct any reported problems, then save the text as UTF-8 with the filename product.json. Check that the editor has not added an extra extension such as .json.txt.

Saving preserves your edit; validating checks a separate property of its contents. A file can save successfully even when its JSON is invalid.

Formatting and validation answer different questions

Operation What it checks or changes
Formatting Adjusts layout, such as indentation and line breaks, to make the text easier to read.
Syntax validation Checks whether the text follows JSON’s rules.
Schema validation Checks whether the data satisfies declared requirements, such as a numeric price or required fields.

A schema describes requirements for data. For example, our fictional catalog could require price to contain a number. JSON Schema provides a way to express and check that requirement. A syntax validator alone does not perform this extra check, and neither check proves that a stated price is factually correct.

Repairing an error is another operation: it changes the input. When a tool offers repair, inspect what changed instead of assuming it only adjusted the layout.

Try a small edit and see what “valid” means

Use the first product example and check the result after each change:

  1. Validate the original. Expected result: valid JSON.
  2. Add a comma after the final true, before the closing brace. Expected result: invalid JSON because of the trailing comma.
  3. Remove that comma. Expected result: valid JSON again.
  4. Put quotation marks around the price, changing 49.9 to "49.9". Expected result: still valid JSON, but the price is now a string.

At step four, the complete document is:

{
  "product": "Notebook",
  "price": "49.9",
  "available": true
}

The catalog’s numeric-price requirement would reject that value even though its JSON syntax is correct. To finish, remove the quotation marks around 49.9, validate once more, and save product.json. You have restored the original numeric price and saved a valid document. Exact error messages vary between tools.

Optional: read and generate JSON with JavaScript

This JavaScript example interprets the product text, changes the price, and generates updated JSON:

const jsonText = '{"product":"Notebook","price":49.9,"available":true}';
const data = JSON.parse(jsonText);

console.log(data.product); // Notebook
data.price = 59.9;

const updatedText = JSON.stringify(data, null, 2);
console.log(updatedText);

The first printed output is Notebook. The second is this JSON text, with two spaces of indentation:

{
  "product": "Notebook",
  "price": 59.9,
  "available": true
}

The outer single quotation marks in the first line delimit a JavaScript string; they are not part of the JSON inside it. The comment also belongs to JavaScript.

JSON.parse() produces a JavaScript value—an object here, although other JSON documents produce other types. JSON.stringify() generates text from compatible values. The 2 requests indentation, and null leaves the optional replacement behavior unused.

Neither function writes a file. This code only prints the results. Serialization also has limits: some JavaScript values are omitted, changed, or rejected, so it is not a universal way to preserve every kind of program data.

Optional: how JSON compares with CSV and XML

The structure you need and the formats the receiving application supports help determine which format to use.

Format Main shape A suitable example
JSON Objects, arrays, and typed values. A product with several colors and a nested seller record.
CSV Rows of separated fields, often with a header row. A simple product table intended for a spreadsheet.
XML Elements and attributes expressed through markup. A document whose content needs explicit structural tags.

CSV’s row-and-field conventions are described in RFC 4180, while the XML specification defines its markup. Each format serves different structures and software requirements; the choice is broader than which one looks shortest.

Try changing one field in the product example, checking its type, and saving the result. You can now follow the information from text to file.

If you work with an API and need to review two versions of its output, follow the guide to comparing JSON API responses.

Share this guide

Article link