JSON Syntax Explained: Rules and Examples

JSON Syntax Explained

JSON syntax defines the rules used to write valid JSON data. Following these rules ensures that applications can correctly parse and exchange information.

Although JSON is simple to learn, small mistakes such as missing quotation marks or extra commas can make an entire document invalid.

Basic JSON Example

{
  "name": "John",
  "age": 30,
  "active": true
}

This example contains three key-value pairs inside a JSON object.

JSON Objects

Objects are enclosed in curly braces and contain key-value pairs separated by commas.

{
  "city": "Paris",
  "country": "France"
}

JSON Arrays

Arrays contain ordered lists of values enclosed in square brackets.

{
  "languages": [
    "JavaScript",
    "Python",
    "Go"
  ]
}

Supported JSON Data Types

TypeExample
String«Hello»
Number123
Booleantrue
Object{«id»:1}
Array[1,2,3]
Nullnull

JSON Syntax Rules

  • Keys must always be enclosed in double quotation marks.
  • Strings must use double quotes.
  • Objects use curly braces.
  • Arrays use square brackets.
  • Items are separated by commas.
  • No trailing commas are allowed.

Invalid JSON Example

{
  name: "John",
  age: 30,
}

This example is invalid because the property names are not enclosed in double quotes and there is a trailing comma.

Valid JSON Example

{
  "name": "John",
  "age": 30
}

Common JSON Syntax Errors

  • Missing quotation marks.
  • Trailing commas.
  • Missing commas.
  • Unclosed braces.
  • Invalid nesting.
  • Using single quotes instead of double quotes.

Why JSON Syntax Matters

Incorrect JSON syntax prevents applications from parsing data correctly. Even a single formatting error can cause an API request or configuration file to fail.

Using a JSON formatter or validator helps detect syntax mistakes before deploying applications.

Frequently Asked Questions

Does JSON require double quotes?

Yes. Property names and string values must use double quotation marks.

Can JSON contain comments?

No. Standard JSON does not support comments.

Can JSON end with a comma?

No. Trailing commas make JSON invalid.

How can I check if my JSON is valid?

You can use a JSON formatter or validator to detect syntax errors automatically.

Related Articles

Related Tools